#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 23;
int a[10], k, n, q, b[10];
long long f[maxn];
int main() {
  cin >> k;
  k = 3 * (k - 1);
  b[0] = 3;
  for (int i = 0; i < 6; i++) scanf("%d", &a[i]);
  for (int i = 1; i < 6; i++) b[i] = b[i - 1] * 10;
  for (int i = 1; i < maxn; i++) {
    int x = i;
    for (int j = 0; j < 6; j++) {
      int y = x % 10;
      if (y % 3 == 0) f[i] += 1ll * y / 3 * a[j];
      x /= 10;
    }
  }
  for (int i = 1, j = k; j > 0; i <<= 1) {
    if (i > j) i = j;
    for (int r = 0; r < 6; r++) {
      for (int w = maxn - 1; w >= 1ll * i * b[r]; w--)
        f[w] = max(f[w], f[w - i * b[r]] + 1ll * i * a[r]);
    }
    j -= i;
  }
  cin >> q;
  while (q--) {
    scanf("%d", &n);
    printf("%lld\n", f[n]);
  }
}
