#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int inf = INT_MAX;
const long long inff = 5e18;
const long long mod = 1e9 + 7;
long long f[7];
long long dp[maxn], dp2[maxn];
vector<long long> value = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3};
void update(long long x, long long dd) {
  for (long long i = 999999; i >= 0; i--)
    if (i >= x) dp[i] = max(dp[i], dp[i - x] + dd);
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long k;
  cin >> k;
  for (long long i = 0; i < 6; i++) cin >> f[i];
  for (long long i = 1; i <= 999999; i++) dp[i] = -inff;
  long long now = 3;
  for (long long i = 0; i < 6; i++) {
    long long cnt = 3 * (k - 1);
    for (long long j = 1;; j <<= 1) {
      if (j >= cnt) {
        update(now * cnt, f[i] * cnt);
        break;
      } else
        update(now * j, f[i] * j), cnt -= j;
    }
    now = now * 10;
  }
  now = 1;
  for (long long i = 0; i < 6; i++) {
    for (long long k = 0; k <= 999999; k++) dp2[k] = dp[k];
    for (long long j = 0; j < 10; j++) {
      for (long long k = 1; k <= 999999; k++)
        if (k >= now * j)
          dp2[k] = max(dp2[k], dp[k - j * now] + value[j] * f[i]);
    }
    for (long long k = 0; k <= 999999; k++) dp[k] = max(dp2[k], dp[k]);
    now = now * 10;
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x;
    cin >> x;
    cout << dp[x] << endl;
  }
}
