#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mo = 998244353;
const long long inf = 1e18;
long long a[N], b[N];
int p[22], tot;
int main() {
  int k;
  scanf("%d", &k);
  int K = min(999999, (k - 1) * 3);
  for (auto i = (0); i <= (20); ++i) {
    if (K <= (1 << i)) {
      p[++tot] = K;
      break;
    } else
      p[++tot] = 1 << i, K -= 1 << i;
  }
  sort(p + 1, p + tot + 1);
  int nw = 1;
  for (auto i = (1); i <= (999999); ++i) a[i] = -inf;
  for (auto o = (0); o <= (5); ++o) {
    long long w;
    scanf("%lld", &w);
    for (auto i = (1); i <= (tot); ++i) {
      if (3ll * p[i] * nw > 999999) break;
      int x = 3 * p[i] * nw;
      for (auto j = (999999); j >= (x); --j)
        a[j] = max(a[j], a[j - x] + p[i] * w);
    }
    for (auto i = (0); i <= (999999); ++i) b[i] = a[i];
    for (auto i = (1); i <= (9); ++i) {
      if (i * nw > 999999) break;
      int x = i * nw;
      long long W = 0;
      if (!(i % 3)) W = i / 3 * w;
      for (auto j = (999999); j >= (x); --j) a[j] = max(a[j], b[j - x] + W);
    }
    nw *= 10;
  }
  int q;
  scanf("%d", &q);
  for (auto i = (1); i <= (q); ++i) {
    int x;
    scanf("%d", &x);
    printf("%lld\n", a[x]);
  }
}
