#include <bits/stdc++.h>
using namespace std;
long long p[6], a[6], f[1000000], d[1000001][2], F, s;
int n = 999999, K, i, j, k, l, I, Q, h, t;
long long js(int t) {
  int i = 0, s;
  long long ans = 0;
  while (t) {
    s = t % 10;
    if (!(s % 3)) ans += a[i] * (s / 3);
    ++i;
    t /= 10;
  }
  return ans;
}
int main() {
  p[0] = 1;
  for (i = 1; i <= 5; i++) p[i] = p[i - 1] * 10;
  scanf("%d", &K);
  for (i = 0; i <= 5; i++) scanf("%lld", &a[i]);
  for (i = 1; i <= n; i++) f[i] = js(i);
  for (l = 0; l <= 5; l++) {
    for (I = 0; I <= 3 * p[l] - 1; I++) {
      h = 1, t = 0;
      for (i = I; i <= n; i += 3 * p[l]) {
        while (h <= t && (i - d[h][0]) > 9 * p[l] * (K - 1)) ++h;
        F = f[i];
        if (h <= t)
          f[i] = (f[i] > d[h][1] + 1ll * i / (3 * p[l]) * a[l]
                      ? f[i]
                      : d[h][1] + 1ll * i / (3 * p[l]) * a[l]);
        s = F - 1ll * i / (3 * p[l]) * a[l];
        while (h <= t && d[t][1] <= s) --t;
        ++t, d[t][0] = i, d[t][1] = s;
      }
    }
  }
  scanf("%d", &Q);
  for (; Q; --Q) {
    scanf("%d", &j);
    printf("%lld\n", f[j]);
  }
  fclose(stdin);
  fclose(stdout);
  return 0;
}
