#include <bits/stdc++.h>
using namespace std;
int q1[1000005];
long long q2[1000005];
void dp(long long *f, long long *g, int n, int k, long long w) {
  for (int i = 0; i < 3; i++) {
    int l = 1, r = 0;
    for (int j = i; j <= n; j += 3) {
      while (l <= r && j - 3 * k > q1[l]) l++;
      if (j % 10 == 0 && f[j / 10] != -1) {
        long long v = f[j / 10] - (j / 3) * w;
        while (l <= r && q2[r] <= v) r--;
        r++;
        q1[r] = j;
        q2[r] = v;
      }
      if (l <= r)
        g[j] = q2[l] + (j / 3) * w;
      else
        g[j] = -1;
    }
  }
}
long long f[1000005], cur1[1000005], cur2[1000005];
int num[6];
int main() {
  int k;
  scanf("%d", &k);
  for (int i = 0; i < 6; i++) scanf("%d", &num[i]);
  memset(f, 255, sizeof(f));
  f[0] = 0;
  int up = 1;
  for (int i = 5; i >= 0; i--) {
    up *= 10;
    dp(f, cur1, up, 3 * k, num[i]);
    dp(f, cur2, up, 3 * (k - 1), num[i]);
    for (int j = 0; j <= up; j++) {
      f[j] = cur1[j];
      for (int k = 0; k < 10 && k <= j; k++) f[j] = max(f[j], cur2[j - k]);
    }
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int x;
    scanf("%d", &x);
    printf("%lld\n", f[x]);
  }
  return 0;
}
