#include <bits/stdc++.h>
template <typename Tp>
void read(Tp &res) {
  static char ch;
  ch = getchar(), res = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) res = res * 10 + ch - 48, ch = getchar();
}
const int MAXN = 1e6 + 19;
int k, f[6], q;
long long dp[MAXN];
inline void chkmax(long long &a, const long long &b) {
  if (b > a) a = b;
}
void push(long long v, long long w) {
  for (int i = int(1e6); i >= w; --i) chkmax(dp[i], dp[i - w] + v);
}
int main() {
  read(k);
  for (int i = 0; i < 6; ++i) read(f[i]);
  for (int i = 0; i <= int(1e6); ++i) {
    int num = i, bit = 0, dig = num % 10;
    while (num) {
      if (dig % 3 == 0) dp[i] += (long long)f[bit] * (dig / 3);
      num /= 10, dig = num % 10, ++bit;
    }
  }
  for (int i = 0, p = 3; i < 6; ++i, p *= 10) {
    int tot = (k - 1) * 3;
    for (int j = 1; j <= tot; j <<= 1)
      push((long long)f[i] * j, (long long)p * j), tot -= j;
    if (tot) push((long long)f[i] * tot, (long long)p * tot);
  }
  read(q);
  while (q--) {
    static int nie;
    read(nie);
    printf("%lld\n", dp[nie]);
  }
  return 0;
}
