#include <bits/stdc++.h>
using namespace std;
const long long maxm = 1e6 + 5;
long long d[maxm];
long long f[maxm];
long long k, q;
signed main() {
  ios::sync_with_stdio(0);
  cin >> k;
  for (long long i = 0; i <= 5; i++) cin >> f[i];
  const long long C = 999999;
  for (long long i = 1; i <= C; i++) d[i] = -1e18;
  d[0] = 0;
  for (long long i = 0, ww = 1; i <= 5; i++, ww *= 10) {
    long long p = 1;
    long long tot = 3 * (k - 1);
    while (p <= tot) {
      tot -= p;
      long long w = 3 * p * ww, v = p * f[i];
      for (long long j = C; j >= w; j--) {
        d[j] = max(d[j], d[j - w] + v);
      }
      p *= 2;
    }
    if (tot) {
      long long w = 3 * tot * ww, v = tot * f[i];
      for (long long j = C; j >= w; j--) {
        d[j] = max(d[j], d[j - w] + v);
      }
    }
    for (long long j = C; j >= 0; j--) {
      for (long long x = 0; x <= 9; x++) {
        long long w = x * ww, v = (x % 3 == 0) * (x / 3 * f[i]);
        if (j >= w) {
          d[j] = max(d[j], d[j - w] + v);
        }
      }
    }
  }
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    cout << d[n] << endl;
  }
  return 0;
}
