#include <bits/stdc++.h>
long long f[1000005], a[6];
inline int read() {
  register int x = 0, f = 1;
  register char s = getchar();
  while (s > '9' || s < '0') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline long long max(const long long &x, const long long &y) {
  return x > y ? x : y;
}
inline int min(const int &x, const int &y) { return x < y ? x : y; }
inline void work(int v, long long w) {
  for (register int i = 999999; i >= v; --i) f[i] = max(f[i], f[i - v] + w);
}
int main() {
  int k = read();
  for (register int i = 0; i <= 5; ++i) a[i] = read();
  for (register int i = 1; i <= 999999; ++i) {
    int cur = i, p = 0;
    while (cur) {
      f[i] += (cur % 10 % 3 == 0) * 1ll * (cur % 10 / 3) * a[p];
      ++p, cur /= 10;
    }
  }
  for (register int i = 0, bas = 1; i <= 5; ++i, bas *= 10) {
    int cur = min(3 * (k - 1), 333333 / bas), x = 1;
    while (x < cur) {
      work(3 * bas * x, 1ll * x * a[i]);
      cur -= x;
      x <<= 1;
    }
    work(3 * bas * cur, 1ll * cur * a[i]);
  }
  int Q = read();
  while (Q--) {
    int vmax = read();
    printf("%lld\n", f[vmax]);
  }
  return 0;
}
