#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
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;
}
const long long maxn = 1e6 + 10;
inline void print(long long *f, long long len) {
  for (long long i = 0; i < len; i++) printf("%lld ", f[i]);
  puts("");
}
long long f[10];
long long dp[maxn];
void psh(long long a, long long b) {
  for (long long i = 1e6, iend = a; i >= iend; --i)
    dp[i] = max(dp[i], dp[i - a] + b);
}
void solve(long long a, long long b) {
  long long nw = min(k, (maxn - 10) / a);
  for (long long i = 1; i < nw; i <<= 1) nw -= i, psh(a * i, b * i);
  psh(a * nw, b * nw);
}
signed main() {
  k = read();
  for (long long i = 0, iend = 5; i <= iend; ++i) f[i] = read();
  k = (k - 1) * 3;
  for (long long i = 1, iend = maxn - 9; i <= iend; ++i) {
    long long nw = 0, p = i;
    while (p) {
      if ((p % 10) % 3 == 0) dp[i] += f[nw] * ((p % 10) / 3);
      ++nw, p /= 10;
    }
  }
  long long nw = 1;
  for (long long i = 0, iend = 5; i <= iend; ++i) {
    solve(nw * 3, f[i]), nw *= 10;
  }
  long long Q = read();
  while (Q--) {
    long long u = read();
    printf("%lld\n", dp[u]);
  }
}
