#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, g[10], q;
long long f[N];
void solve(int sum, int v) {
  int m = min(n, (int)1000000.0 / sum);
  for (int i = 1; i < m; i <<= 1) {
    m -= i;
    for (int j = 1000000; j >= sum * i; j--)
      f[j] = max(f[j], f[j - sum * i] + 1ll * v * i);
  }
  for (int i = 1000000; i >= sum * m; i--)
    f[i] = max(f[i], f[i - sum * m] + 1ll * v * m);
  return;
}
int main() {
  scanf("%d", &n);
  n = 3 * (n - 1);
  for (int i = 0; i <= 5; i++) scanf("%d", &g[i]);
  for (int i = 0; i <= 999999; i++) {
    int now = i, stp = 0;
    while (now) {
      int op = now % 10;
      if (op % 3 == 0) f[i] += 1ll * g[stp] * op / 3;
      stp++;
      now /= 10;
    }
  }
  for (int i = 0, sum = 3; i <= 5; i++, sum *= 10) solve(sum, g[i]);
  scanf("%d", &q);
  while (q--) {
    int x;
    scanf("%d", &x);
    printf("%lld\n", f[x]);
  }
  return 0;
}
