#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
inline long long lowbit(long long x) { return x & (-x); }
long long f[1000010], g[11], p[11];
long long k, Maxn = 1e6;
inline void calc(long long v, long long w) {
  for (register long long i = Maxn; i >= w; i--) f[i] = max(f[i], f[i - w] + v);
}
inline void upd(long long v, long long w) {
  long long num = min(3 * (k - 1), Maxn / w);
  for (register long long i = 0; i <= 20; i++)
    if ((1 << i) <= num) {
      num -= (1 << i);
      calc(v * (1 << i), w * (1 << i));
    }
  calc(num * v, num * w);
}
int main() {
  k = read();
  for (register long long i = 0; i <= 5; i++) g[i] = read();
  p[0] = 1;
  for (register long long i = 1; i <= 5; i++) p[i] = p[i - 1] * 10;
  for (register long long i = 0; i <= Maxn; i++) {
    long long t = i, s = 0, now = 0;
    while (t) {
      long long tmp = t % 10;
      if (tmp % 3 == 0) {
        s += tmp / 3 * g[now];
      }
      t /= 10;
      now++;
    }
    f[i] = s;
  }
  for (register long long i = 0; i <= 5; i++) upd(g[i], p[i] * 3);
  long long q = read();
  while (q--) {
    cout << f[read()] << '\n';
  }
  return 0;
}
