#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 6;
long long dp[maxn], f[7], pw[7];
int k, q, n;
int main() {
  scanf("%d", &k);
  k = 3 * (k - 1);
  for (int i = 0; i < 6; i++) scanf("%lld", &f[i]);
  pw[0] = 1;
  for (int i = 1; i < 6; i++) pw[i] = pw[i - 1] * 10;
  for (int i = 0; i <= 999999; i++) {
    long long x = i;
    for (int j = 0; j < 6; j++) {
      if (x % 10 % 3 == 0) dp[i] += x % 10 / 3 * f[j];
      x /= 10;
    }
  }
  for (int x = 0; x < 6; x++) {
    for (int i = 1, j = k; j > 0; i *= 2) {
      if (i > j) i = j;
      for (int j = 999999; j >= pw[x] * i * 3; j--) {
        dp[j] = max(dp[j], dp[j - pw[x] * i * 3] + i * f[x]);
      }
      j -= i;
    }
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d", &n);
    printf("%lld\n", dp[n]);
  }
  return 0;
}
