#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
long long f[N], p[10];
int n, m, k;
void work(int v, long long w) {
  for (register int i = 999999; i >= v; --i) f[i] = max(f[i], f[i - v] + w);
}
int main() {
  scanf("%d", &k);
  for (int i = 0; i <= 5; i++) {
    scanf("%d", &p[i]);
  }
  for (int i = 1; i <= 999999; i++) {
    int x = i, s, now = 0;
    while (x) {
      s = x % 10;
      if (s % 3 == 0) f[i] += p[now] * (s / 3) * 1ll;
      x = x / 10;
      now++;
    }
  }
  for (int i = 0, bas = 1; i <= 5; i++, bas *= 10) {
    int cur = min(3 * (k - 1), 333333 / bas);
    for (int x = 1; x < cur; cur -= x, x <<= 1) {
      work(3 * bas * x, 1ll * x * p[i]);
    }
    if (cur) work(3 * bas * cur, 1ll * cur * p[i]);
  }
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m);
    cout << f[m] << endl;
  }
}
