#include <bits/stdc++.h>
using namespace std;
const int maxd = 6;
const long long minf = -1e18;
void upd(long long& a, const long long& b) { a = max(a, b); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<int> p10(maxd + 1, 1);
  for (int i = 1; i < maxd + 1; i++) p10[i] = p10[i - 1] * 10;
  int k;
  cin >> k;
  vector<long long> f(maxd);
  for (int i = 0; i < maxd; i++) cin >> f[i];
  vector<vector<long long> > dp(maxd + 1, vector<long long>(p10.back(), minf));
  dp[maxd][0] = 0;
  for (int i = maxd - 1; i >= 0; i--) {
    if (k > 3) {
      for (int rem = 0; rem <= p10[i] * 8; rem += p10[i]) {
        deque<pair<long long, int> > q;
        for (int j = rem; j < p10.back(); j += p10[i] * 9) {
          while (!q.empty() && q.front().second + p10[i] * 9 * (k - 3) < j)
            q.pop_front();
          if (dp[i + 1][j] != minf) {
            pair<long long, int> p(
                dp[i + 1][j] - f[i] * 3ll * (j / (p10[i] * 9)), j);
            while (!q.empty() && q.back().first < p.first) q.pop_back();
            q.push_back(p);
          }
          if (q.size())
            upd(dp[i][j], q.front().first + f[i] * 3ll * (j / (p10[i] * 9)));
        }
      }
    }
    for (int j = 0; j < p10.back(); j++) upd(dp[i][j], dp[i + 1][j]);
    for (int it = 0; it < min(k, 3); it++) {
      for (int j = p10.back() - 1; j >= 0; j--) {
        for (int lastd = 0; lastd <= 9 && lastd * p10[i] <= j; lastd++) {
          if (dp[i][j - lastd * p10[i]] == minf) continue;
          long long val = (lastd % 3 == 0 ? f[i] * 1ll * (lastd / 3) : 0);
          upd(dp[i][j], dp[i][j - lastd * p10[i]] + val);
        }
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int ni;
    cin >> ni;
    cout << dp[0][ni] << "\n";
  }
  return 0;
}
