#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 10;
int k, F[6], q, n, top;
long long f[maxn], w[2009], v[2009];
long long is(int x) {
  long long ans = 0;
  for (int i = 0; i <= 5; i++, x /= 10) {
    if (x % 10 % 3 == 0) ans += 1ll * (x % 10) / 3 * F[i];
  }
  return ans;
}
int main() {
  scanf("%d", &k);
  for (int i = 0; i <= 5; i++) scanf("%d", &F[i]);
  for (int i = 1; i <= 999999; i++) f[i] = is(i);
  for (int i = 0, dig = 1; i <= 5; i++, dig *= 10) {
    int num = 3 * (k - 1);
    for (int j = 1; j <= num; num -= j, j *= 2)
      v[++top] = 1ll * dig * 3 * j, w[top] = 1ll * F[i] * j;
    if (!num) continue;
    v[++top] = 1ll * dig * 3 * num, w[top] = 1ll * F[i] * num;
  }
  for (int i = 1; i <= top; i++)
    for (int j = 1000000; j >= v[i]; j--) f[j] = max(f[j], f[j - v[i]] + w[i]);
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    printf("%lld\n", f[n]);
  }
}
