#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 9, M = 1e6;
long long f[N], k, c[7], pw = 1;
long long q;
void dp(long long w, long long v) {
  long long rest = k;
  for (long long i = 1; i < rest; i <<= 1) {
    for (long long j = M; j >= w * i; j--)
      f[j] = max(f[j], f[j - 1ll * w * i] + 1ll * v * i);
    rest -= i;
  }
  for (long long j = M; j >= w * rest; j--)
    f[j] = max(f[j], f[j - w * rest] + rest * v);
}
signed main() {
  scanf("%lld", &k);
  k = 3 * (k - 1);
  for (long long i = 0; i < 6; i++) scanf("%lld", &c[i]);
  for (long long i = 0, pos = 0; i < M; i++) {
    long long x = i, now = x % 10;
    pw = 0;
    while (x) {
      if (now % 3 == 0) f[i] += 1ll * c[pw] * (now / 3);
      x /= 10;
      pw++;
      now = x % 10;
    }
  }
  pw = 1;
  for (long long i = 0; i < 6; i++) dp(pw * 3, c[i]), pw *= 10;
  scanf("%lld", &q);
  while (q--) {
    long long x;
    scanf("%lld", &x);
    printf("%lld\n", f[x]);
  }
  return 0;
}
