#include <bits/stdc++.h>
using namespace std;
long long k, f[6], q, n, dp1[7][1000005], dp2[7][1000005], tmp, lazy, succ,
    opz[] = {1, 2, 4, 5, 7, 8};
deque<pair<long long, long long> > v;
long long query() { return v.front().first; }
void del(long long pos) {
  while (v.size() > 0 && v.front().second <= pos) v.pop_front();
}
void add(long long val, long long pos) {
  while (v.size() > 0 && v.back().first <= val) {
    v.pop_back();
  }
  v.push_back({val, pos});
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> k;
  for (int i = 0; i < 6; i++) cin >> f[i];
  for (int i = 0; i < 1000000; i++) {
    dp1[6][i] = 0;
    dp2[6][i] = -1000000000000000ll;
  }
  dp2[6][0] = 0;
  tmp = 300000;
  for (long long i = 5; i >= 0; i--) {
    for (long long j = 0; j < tmp; j++) {
      lazy = 0;
      v.clear();
      for (long long z = j; z < 1000000; z += tmp) {
        succ = dp2[i + 1][z];
        if (z - tmp / 3 >= 0) succ = max(succ, dp2[i + 1][z - tmp / 3]);
        if (z - (tmp / 3) * 2 >= 0)
          succ = max(succ, dp2[i + 1][z - (tmp / 3) * 2]);
        add(succ - lazy, z);
        dp2[i][z] = query() + lazy;
        if (z - (k - 1) * 3ll * tmp - tmp >= 0)
          dp2[i][z] = max(dp2[i][z], dp2[i + 1][z - (k - 1) * 3ll * tmp - tmp] +
                                         f[i] * ((k - 1) * 3 + 1));
        if (z - (k - 1) * 3ll * tmp - 2 * tmp >= 0)
          dp2[i][z] =
              max(dp2[i][z], dp2[i + 1][z - (k - 1) * 3ll * tmp - 2 * tmp] +
                                 f[i] * ((k - 1) * 3 + 2));
        if (z - (k - 1) * 3ll * tmp - 3 * tmp >= 0)
          dp2[i][z] =
              max(dp2[i][z], dp2[i + 1][z - (k - 1) * 3ll * tmp - 3 * tmp] +
                                 f[i] * ((k - 1) * 3 + 3));
        for (int p = 0; p < 6; p++) {
          if (z - (k - 1) * 3ll * tmp - (tmp / 3) * opz[p] >= 0)
            dp2[i][z] =
                max(dp2[i][z],
                    dp2[i + 1][z - (k - 1) * 3ll * tmp - (tmp / 3) * opz[p]] +
                        f[i] * (k - 1) * 3);
        }
        del(z - (k - 1) * 3ll * tmp);
        lazy += f[i];
      }
    }
    tmp /= 10;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    cout << dp2[0][n] << "\n";
  }
  return 0;
}
