#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
int main() {
  ios::sync_with_stdio(false);
  int k;
  cin >> k;
  vector<long long> f(6, 0);
  for (int i = 0; i < 6; i++) cin >> f[i];
  vector<long long> ten(10, 1);
  for (int i = 1; i < 6; i++) ten[i] = ten[i - 1] * 10;
  int sz = 1000000;
  vector<long long> dp(sz, -large);
  dp[0] = 0;
  for (int d = 0; d < 6; d++) {
    long long g = 1;
    long long l = 3 * (k - 1);
    while (l > 0) {
      g = min(g, l);
      long long c = 3 * g * ten[d];
      long long val = f[d] * g;
      for (int i = sz - 1; i >= c; i--) dp[i] = max(dp[i], dp[i - c] + val);
      l -= g;
      g <<= 1;
    }
  }
  for (int d = 0; d < 6; d++) {
    for (int i = sz - 1; i >= 0; i--) {
      for (int j = 1; j < 10; j++) {
        long long c = j * ten[d];
        long long val = 0;
        if (j % 3 == 0) val = j / 3 * f[d];
        if (i - c >= 0) dp[i] = max(dp[i], dp[i - c] + val);
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    cout << dp[n] << endl;
  }
  return 0;
}
