#include <bits/stdc++.h>
using namespace std;
int K;
signed long long F[6];
int Q;
int N;
signed long long dp[1010101];
signed long long p10[8];
int FS[10] = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3};
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  p10[0] = 1;
  for (i = 0; i < (6); i++) p10[i + 1] = p10[i] * 10;
  cin >> K;
  for (i = 0; i < (6); i++) cin >> F[i];
  for (i = 0; i < (1000001); i++) dp[i] = -1LL << 60;
  dp[0] = 0;
  for (i = 0; i < (6); i++) {
    int cand = 3 * (K - 1);
    int cur = 1;
    while (cand) {
      int num = min(cand, cur);
      signed long long sc = num * F[i];
      signed long long v = num * 3 * p10[i];
      for (j = 999999; j >= v; j--) dp[j] = max(dp[j], dp[j - v] + sc);
      cand -= num;
      cur *= 2;
    }
  }
  for (i = 0; i < (6); i++) {
    for (j = 999999; j >= 0; j--) {
      for (x = 1; x <= 9; x++)
        if (j >= x * p10[i])
          dp[j] = max(dp[j], dp[j - x * p10[i]] + FS[x] * F[i]);
    }
  }
  cin >> Q;
  while (Q--) {
    cin >> N;
    cout << dp[N] << endl;
  }
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
