#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int f[6], k, m, q, n;
long long w[N], v[N], dp[N];
void add(long long num, int v1, int w1) {
  long long tp = 1;
  while (tp <= num) {
    v[++m] = tp * v1;
    w[m] = tp * w1;
    num -= tp;
    tp *= 2;
  }
  if (num) {
    v[++m] = num * v1;
    w[m] = num * w1;
  }
}
int main() {
  cin >> k;
  k = 3 * (k - 1);
  memset(dp, 128, sizeof dp);
  dp[0] = 0;
  for (int i = 0; i <= 5; i++) scanf("%d", &f[i]);
  for (int i = 1, j = 0; i <= 100000; i *= 10, j++) add(k, i * 3, f[j]);
  for (int i = 1; i <= m; i++)
    for (int j = N - 6; j >= v[i]; j--) dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
  for (int i = 0, no = 1; i <= 5; i++, no *= 10) {
    for (int j = N - 6; j >= no; j--) {
      for (int p = 1; p <= 9; p++) {
        if (j < p * no) break;
        dp[j] = max(dp[j], dp[j - p * no] + 1LL * (!(p % 3)) * p / 3 * f[i]);
      }
    }
  }
  cin >> q;
  while (q--) scanf("%d", &n), printf("%lld\n", dp[n]);
}
