#include <bits/stdc++.h>
using namespace std;
int k;
long long p[6];
int q;
long long f[1000001];
long long w[1000001];
long long c[1000001];
int power = 3;
int tot;
int main() {
  cin >> k;
  for (int i = 1; i <= 999999; i++) {
    f[i] = -0x7ffffffffffff;
  }
  for (int i = 0; i <= 5; i++) {
    cin >> p[i];
  }
  for (int i = 0; i <= 5; i++) {
    long long t = 3 * (k - 1);
    long long now = 1;
    while (t > now) {
      t -= now;
      w[++tot] = p[i] * now;
      c[tot] = power * now;
      now *= 2;
    }
    w[++tot] = p[i] * t;
    c[tot] = power * t;
    power *= 10;
  }
  for (int i = 1; i <= tot; i++) {
    for (int j = 999999; j >= c[i]; j--) {
      f[j] = max(f[j], f[j - c[i]] + w[i]);
    }
  }
  power = 1;
  for (int i = 0; i <= 5; i++) {
    for (int j = 999999; j >= 0; j--) {
      for (int x = 1; x <= 9; x++) {
        if (j < x * power) {
          break;
        }
        f[j] = max(f[j], f[j - x * power] + (!(x % 3)) * (x / 3) * p[i]);
      }
    }
    power *= 10;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int n;
    cin >> n;
    cout << f[n] << endl;
  }
  return 0;
}
