#include <bits/stdc++.h>
using namespace std;
const int MX = 1000000;
const long long inf = 1e18;
int k, q;
long long dp[6][MX], a[6];
deque<pair<long long, int> > dq;
void insert(long long x, int i) {
  while (dq.size() && dq.back().first <= x) dq.pop_back();
  dq.emplace_back(x, i);
}
long long query(int i) {
  while (dq.front().second <= i - (3 * k - 2)) dq.pop_front();
  return dq.front().first;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> k;
  for (int i = 0; i < (6); i++) cin >> a[i];
  for (int i = 0; i < (MX); i++) dp[0][i] = -inf;
  for (int q = 0; q < (3 * (k - 1) + 1); q++) {
    int i = 3 * q;
    if (i >= MX) break;
    long long aux = q * a[0];
    for (int d = 0; d < (10); d++) {
      int x = i + d;
      if (x >= MX) break;
      long long off = d % 3 == 0 ? a[0] * d / 3 : 0;
      dp[0][x] = max(dp[0][x], aux + off);
    }
  }
  for (int j = 1, f = 30; j < 6; j++, f *= 10) {
    for (int i = 0; i < (MX); i++) dp[j][i] = -inf;
    for (int r = 0; r < (f); r++) {
      while (dq.size()) dq.pop_back();
      for (int i = r, q = 0; i < MX; i += f, q++) {
        insert(dp[j - 1][i] - q * a[j], q);
        long long aux = query(q) + q * a[j];
        for (int d = 0; d < (10); d++) {
          int x = i + d * (f / 3);
          if (x >= MX) break;
          long long off = d % 3 == 0 ? a[j] * d / 3 : 0;
          dp[j][x] = max(dp[j][x], aux + off);
        }
      }
    }
  }
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    cout << dp[5][n] << '\n';
  }
  return 0;
}
