#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
long long n, k, m, a[N], f[N];
inline long long read() {
  long long ret = 0, f = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) {
    ret = ret * 10 + c - 48;
    c = getchar();
  }
  if (f) return -ret;
  return ret;
}
void solve(long long n, long long m) {
  long long lim = min(3 * k - 3, (long long)1e6 / n);
  for (long long i = 1; i < lim; i = i * 2) {
    for (long long j = 999999; j >= n * i; j--)
      f[j] = max(f[j], f[j - n * i] + m * i);
    lim -= i;
  }
  for (long long j = 999999; j >= n * lim; j--)
    f[j] = max(f[j], f[j - n * lim] + m * lim);
}
signed main() {
  k = read();
  for (long long i = 0; i <= 5; i++) a[i] = read();
  for (long long i = 0; i <= 999999; i++) {
    long long x = i, y = 0;
    while (x) {
      long long z = x % 10;
      if (z % 3 == 0) f[i] += a[y] * (z / 3);
      x = x / 10;
      y++;
    }
  }
  long long x = 3;
  for (long long i = 0; i <= 5; i++) {
    solve(x, a[i]);
    x = x * 10;
  }
  m = read();
  while (m--) printf("%lld\n", f[read()]);
  return 0;
}
