#include <bits/stdc++.h>
using namespace std;
const long long pw10[] = {1, 10, 100, 1000, 10000, 100000};
long long K, F[7], Q, dp[7][2000000];
signed main() {
  cin >> K;
  for (long long i = 1; i <= 6; ++i) cin >> F[i];
  memset(dp, -0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (long long i = 1; i <= 6; ++i) {
    for (long long j = 0; j < 3 * pw10[i - 1]; ++j) {
      deque<pair<long long, long long> > q;
      for (long long k = j, cnt = 0; k < 1e6; k += 3 * pw10[i - 1], ++cnt) {
        long long val = dp[i - 1][k] - cnt * F[i];
        while (q.size() && q.back().first <= val) q.pop_back();
        q.push_back(make_pair(val, cnt));
        if (cnt - q.front().second > 3 * K) q.pop_front();
        dp[i][k] = max(dp[i][k], cnt * F[i] + q.front().first);
        long long pos = 0;
        while (cnt - q[pos].second > 3 * (K - 1)) ++pos;
        long long v = cnt * F[i] + q[pos].first;
        for (long long t = 0; t < 10; ++t)
          dp[i][k + t * pw10[i - 1]] = max(dp[i][k + t * pw10[i - 1]], v);
      }
    }
  }
  for (cin >> Q; Q; --Q) {
    long long x;
    cin >> x;
    cout << max(0ll, dp[6][x]) << '\n';
  }
  return 0;
}
