#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
int a, b, c, d, n, m, k;
long long pc[1000001];
long long dp[7][333335];
int f[6];
const int N = 333333;
const long long infll = (long long)1000000000 * 1000000000;
inline void upd(long long& a, long long b) {
  if (a < b) a = b;
}
long long solve(int n) {
  long long ans = pc[n];
  for (int _n((n)-1), i(0); i <= _n; i++) {
    int rem = n - i;
    if (rem % 3) continue;
    ans = max(ans, pc[i] + dp[5][rem / 3]);
  }
  return ans;
}
int main() {
  scanf("%d", &k);
  --k;
  for (int _n((6) - 1), i(0); i <= _n; i++) scanf("%d", &f[i]);
  pc[0] = 0;
  for (int _n((999999)), i(1); i <= _n; i++) {
    int t = i;
    long long a = 0;
    for (int _n((6) - 1), j(0); j <= _n; j++) {
      int dig = t % 10;
      if (dig == 3 || dig == 6 || dig == 9) {
        a += (long long)f[j] * (dig / 3);
      }
      t /= 10;
    }
    pc[i] = a;
  }
  for (int _n((7) - 1), i(0); i <= _n; i++)
    for (int _n((N + 2) - 1), j(0); j <= _n; j++) dp[i][j] = -8 * infll;
  for (int _n((N + 1) - 1), i(0); i <= _n; i++) {
    if (i > 3 * k) break;
    dp[0][i] = (long long)f[0] * i;
  }
  auto ev1 = [&](int i, int s) {
    return (long long)i * (f[1] - 10LL * f[0]) + (long long)s * f[0];
  };
  for (int _n((N + 1) - 1), i(0); i <= _n; i++) {
    if (i > 33LL * k) break;
    int l = 0;
    if (i > 3 * k) {
      int rem = i - 3 * k;
      l = rem / 10;
      if (rem % 10) ++l;
    }
    int r = min(3 * k, i / 10);
    if (l > r) continue;
    dp[1][i] = max(ev1(l, i), ev1(r, i));
  }
  for (int _n(5), d(2); d <= _n; d++) {
    long long o = 0;
    for (int _n((d + 1) - 1), z(0); z <= _n; z++) o = o * 10 + 3;
    o *= k;
    o = min<long long>(o, N);
    int t = 1;
    for (int _n((d)-1), j(0); j <= _n; j++) t *= 10;
    for (int _n((o + 1) - 1), i(0); i <= _n; i++) {
      int r = min(3 * k, i / t);
      long long cd = 0;
      for (int _n((r + 1) - 1), j(0); j <= _n; j++) {
        upd(cd, dp[d - 1][i - j * t] + (long long)f[d] * j);
      }
      dp[d][i] = cd;
    }
  }
  scanf("%d", &m);
  for (int _n((m)), hod(1); hod <= _n; hod++) {
    scanf("%d", &n);
    long long ans = solve(n);
    printf("%I64d\n", ans);
  }
}
