#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
inline int read() {
  int x = 0, f = 1;
  char ch = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
  return x * f;
}
long long f[6], k, Q, id[6], sum[6];
pair<long long, long long> things[MAXN >> 6];
long long mi10[6], numt, dp[2][MAXN];
inline int cal(int x) {
  if (x >= 10) x %= 10;
  if (x % 3)
    return 0;
  else
    return (x / 3);
}
int main() {
  scanf("%lld", &k);
  for (int i = 0; i < 6; ++i) scanf("%lld", &f[i]);
  mi10[0] = 1;
  for (int i = 1; i < 6; ++i) mi10[i] = mi10[i - 1] * 10;
  scanf("%lld", &Q);
  int lst = (k - 1) * 3;
  for (int i = 0; i < 22; ++i) {
    if (lst > 1 << i) {
      lst -= 1 << i;
      for (int j = 0; j < 6; ++j)
        if (mi10[j] * (1 << i) * 3 < MAXN - 10)
          things[++numt] = make_pair(mi10[j] * (1 << i) * 3, f[j] * (1 << i));
    } else {
      for (int j = 0; j < 6; ++j)
        if (mi10[j] * lst * 3 < MAXN - 10)
          things[++numt] = make_pair(mi10[j] * lst * 3, f[j] * lst);
      break;
    }
  }
  int x = 999999;
  memset(dp, 0xcf, sizeof(dp));
  dp[0][0] = 0;
  for (int j = 1; j <= numt; ++j) {
    for (int i = x; i >= things[j].first; i -= 3) {
      dp[0][i] = max(dp[0][i], dp[0][i - things[j].first] + things[j].second);
    }
  }
  numt = 0;
  for (int h = 0; h < 6; ++h) {
    for (int i = x; i >= 0; --i) {
      for (int digit = 0; digit < 10 && i - digit * mi10[h] >= 0; ++digit)
        if (digit % 3)
          dp[0][i] = max(dp[0][i], dp[0][i - digit * mi10[h]]);
        else
          dp[0][i] =
              max(dp[0][i], dp[0][i - digit * mi10[h]] + f[h] * (digit / 3));
    }
  }
  for (int kase = 1; kase <= Q; ++kase) {
    x = read();
    printf("%lld\n", dp[0][x]);
  }
}
