#include <bits/stdc++.h>
const long long max_N = 1100005;
const long long _max_N = 1000000;
const long long INF = 1e18;
long long K, q, val[6], dp[max_N];
long long fortune[10] = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3};
long long power10[6] = {1, 10, 100, 1000, 10000, 100000};
long long read() {
  char c = getchar();
  long long ans = 0;
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans;
}
void Write(long long x) {
  if (x < 10)
    putchar(x + '0');
  else
    Write(x / 10), putchar(x % 10 + '0');
}
long long min(long long x, long long y) { return x < y ? x : y; }
long long max(long long x, long long y) { return x > y ? x : y; }
void Insert(long long x, long long y) {
  for (long long i = _max_N; i >= x; i--) dp[i] = max(dp[i], dp[i - x] + y);
}
void Insert_all(long long x, long long y, long long num) {
  for (long long i = 1; num; i <<= 1) {
    if (i > num) i = num;
    num -= i;
    Insert(i * x, i * y);
  }
}
int main() {
  K = read();
  for (long long i = 0; i < 6; i++) val[i] = read();
  for (long long i = 1; i <= _max_N; i++) dp[i] = -INF;
  for (long long d = 0; d < 6; d++)
    Insert_all(power10[d] * 3, val[d], (K - 1) * 3);
  for (long long d = 0; d < 6; d++)
    for (long long j = _max_N; j >= 0; j--)
      for (long long i = 1; i < 10 && j >= i * power10[d]; i++)
        dp[j] = max(dp[j], dp[j - i * power10[d]] + fortune[i] * val[d]);
  q = read();
  while (q--) Write(dp[read()]), putchar('\n');
  return 0;
}
