#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int f[6], k, m, q, n[N >> 3], mx;
long long dp[N], tp;
void done(long long v, long long w) {
  for (int i = mx; i >= v; i--) dp[i] = max(dp[i], dp[i - v] + w);
}
void add(long long num, int v, int w) {
  tp = 1;
  while (tp <= num) {
    done(tp * v, tp * w);
    num -= tp;
    tp <<= 1;
  }
  if (num) done(num * v, num * w);
}
int main() {
  cin >> k;
  k = 3 * (k - 1);
  for (int i = 0; i <= 5; i++) scanf("%d", &f[i]);
  cin >> q;
  for (int i = 1; i <= q; i++) scanf("%d", &n[i]), mx = max(mx, n[i]);
  for (int i = 1; i <= mx; i++) {
    int x = i, kp, j = 0;
    while (x) {
      kp = x % 10;
      if (kp % 3 == 0) dp[i] += 1LL * kp / 3 * f[j];
      x /= 10;
      j++;
    }
  }
  for (int i = 3, j = 0; j <= 5; i *= 10, j++) add(min(k, mx / i), i, f[j]);
  for (int i = 1; i <= q; i++) printf("%lld\n", dp[n[i]]);
}
