#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
const long long inf = (1ll << 58ll);
int k, n, q;
int ten[10] = {1, 10, 100, 1000, 10000, 100000};
long long F[7];
long long dp[maxn], g[7][maxn];
long long read() {
  long long s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
int main() {
  k = read();
  for (int i = 0; i < 6; ++i) F[i] = read();
  fill(dp, dp + 1 + 999999, -inf);
  dp[0] = 0;
  for (int d = 1; d <= 6; ++d) {
    long long left = 1ll * 3 * (k - 1);
    long long group = 1ll;
    while (left > 0) {
      group = min(group, left);
      long long val = 1ll * group * F[d - 1];
      long long wei = 1ll * 3 * group * ten[d - 1];
      for (int i = 999999; i >= wei; --i) {
        dp[i] = max(dp[i], dp[i - wei] + val);
      }
      left -= group;
      group *= 2;
    }
  }
  for (int i = 1; i <= 6; ++i) fill(g[i], g[i] + 1000000, -inf);
  for (int i = 0; i <= 999999; ++i) g[0][i] = dp[i];
  for (int d = 1; d <= 6; ++d) {
    for (int j = 0; j <= 9; ++j) {
      long long val, wei;
      if (j == 3 || j == 6 || j == 9)
        val = 1ll * (j / 3) * F[d - 1];
      else
        val = 0;
      wei = 1ll * j * ten[d - 1];
      for (int i = 999999; i >= wei; i--)
        g[d][i] = max(g[d][i], g[d - 1][i - wei] + val);
    }
  }
  q = read();
  for (int i = 1; i <= q; ++i) {
    n = read();
    printf("%lld\n", g[6][n]);
  }
  return 0;
}
