#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch = getchar();
  int res = 0, flag = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res * flag;
}
int k, F[6], q, x;
long long f[1000000];
int main() {
  k = read();
  for (register int i = 0; i < 6; ++i) F[i] = read();
  for (register int i = 1; i < 1000000; ++i) {
    register int tmp = i, ws = 0;
    while (tmp) {
      f[i] += 1ll * (tmp % 10 % 3 == 0) * (tmp % 10 / 3) * F[ws];
      ++ws;
      tmp /= 10;
    }
  }
  int power = 1;
  for (register int i = 0; i < 6; ++i) {
    register int t = min(3 * (k - 1), 333333 / power), now = 1;
    while (now < t) {
      for (register int j = 999999; j >= 3 * power * now; --j)
        f[j] = max(f[j], f[j - 3 * power * now] + 1ll * now * F[i]);
      t -= now;
      now <<= 1;
    }
    for (register int j = 999999; j >= 3 * power * t; --j)
      f[j] = max(f[j], f[j - 3 * power * t] + 1ll * t * F[i]);
    power *= 10;
  }
  q = read();
  while (q--) {
    x = read();
    printf("%lld\n", f[x]);
  }
  return 0;
}
