#include <bits/stdc++.h>
using namespace std;
const long long N = 999999 + 7;
const long long INF = (long long)1e18;
long long n;
long long k;
long long q;
long long sum;
long long f[6];
long long val[100];
long long best[N];
long long get(long long x) {
  long long p = 0, sol = 0;
  while (x) {
    long long cif = x % 10;
    if (cif % 3 == 0) {
      sol += (cif / 3) * f[p];
    }
    x /= 10;
    p++;
  }
  return sol;
}
struct T {
  long long profit;
  long long ocup;
};
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> k >> f[0] >> f[1] >> f[2] >> f[3] >> f[4] >> f[5] >> q;
  long long lim = log2(3 * (k - 1));
  for (long long i = 0; i < lim; i++) {
    val[i] = (1 << i);
  }
  val[lim] = 3 * (k - 1);
  for (long long i = 0; i < lim; i++) {
    val[lim] -= val[i];
  }
  for (long long i = 1; i < N; i++) {
    best[i] = -INF;
  }
  for (long long j = 0; j <= lim; j++) {
    long long ocup = 3 * val[j];
    for (long long ob = 0; ob <= 5; ob++) {
      long long profit = (long long)val[j] * f[ob];
      for (long long i = N - ocup - 1; i >= 0; i--) {
        best[i + ocup] = max(best[i + ocup], best[i] + profit);
      }
      ocup *= 10;
    }
  }
  for (long long i = 1; i <= q; i++) {
    long long sol = -INF;
    cin >> sum;
    for (long long x = 0; x <= sum; x++) {
      sol = max(sol, get(x) + best[sum - x]);
    }
    cout << sol << "\n";
  }
}
