#include <bits/stdc++.h>
using namespace std;
long long k, a[10000], f[1000000], n;
signed main() {
  scanf("%lld", &k);
  for (long long i = 0; i < 6; i++) scanf("%lld", &a[i]);
  for (long long i = 1; i <= 999999; i++) {
    long long x = i, j = 0;
    while (x) {
      long long y = x % 10;
      if (y % 3 == 0) f[i] += y / 3 * a[j];
      x /= 10;
      ++j;
    }
  }
  for (long long i = 0, p = 1; i <= 5; i++, p *= 10) {
    long long x = min(3 * (k - 1), 333333 / p), y = 1;
    while (x > y) {
      for (long long j = 999999; j >= 3 * p * y; j--)
        f[j] = max(f[j], f[j - 3 * p * y] + y * a[i]);
      x -= y;
      y <<= 1;
    }
    for (long long j = 999999; j >= 3 * p * x; j--)
      f[j] = max(f[j], f[j - 3 * p * x] + x * a[i]);
  }
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    long long x;
    scanf("%lld", &x);
    printf("%lld\n", f[x]);
  }
  return 0;
}
