#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e6 + 100;
const long long inf = 0x3f3f3f3f;
const long long iinf = 1 << 30;
const long long linf = 2e18;
const long long mod = 998244353;
const double eps = 1e-7;
template <class T = long long>
T chmin(T &a, T b) {
  return a = min(a, b);
}
template <class T = long long>
T chmax(T &a, T b) {
  return a = max(a, b);
}
template <class T = long long>
inline void red(T &x) {
  x -= mod, x += x >> 31 & mod;
}
template <class T = long long>
T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}
long long m = 1e6, k, q;
long long p[6], f[maxn];
void insert(long long v, long long w) {
  for (long long i = (m); i >= (v); --i) chmax(f[i], w + f[i - v]);
}
void add(long long v, long long w) {
  long long now = min(k, m / v);
  for (long long i = 1; i < now; i <<= 1) now -= i, insert(v * i, w * i);
  insert(v * now, w * now);
}
signed main() {
  k = read(), k = 3 * (k - 1);
  for (long long i = (0); i <= (5); ++i) p[i] = read();
  for (long long i = (0); i <= (m); ++i) {
    long long x = i;
    for (long long j = (0); j <= (5); ++j) {
      if ((x % 10) % 3 == 0) f[i] += p[j] * ((x % 10) / 3);
      x /= 10;
    }
  }
  long long tot = 3;
  for (long long i = (0); i <= (5); ++i) add(tot, p[i]), tot *= 10;
  q = read();
  while (q--) {
    long long n = read();
    printf("%lld\n", f[n]);
  }
  return 0;
}
