#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int k, q;
long long f[10], dp[maxn];
void update(long long v, long long w) {
  for (int i = 1000000; i >= w; i--) dp[i] = max(dp[i], dp[i - w] + v);
  return;
}
void solve(long long v, long long w) {
  int num = min((long long)k, 1000000 / w);
  for (int i = 0; (1 << i) <= num; i++) {
    int c = (1 << i);
    update(v * c, w * c);
    num -= c;
  }
  if (num) update(v * num, w * num);
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> k;
  k = 3 * (k - 1);
  for (int i = 0; i <= 5; i++) cin >> f[i];
  for (int i = 0; i <= 1000000; i++) {
    int cur = 0, x = i;
    while (x) {
      int low = x % 10;
      if (low % 3 == 0) {
        dp[i] += f[cur] * (low / 3);
      }
      x /= 10;
      ++cur;
    }
  }
  int sz = 1;
  for (int i = 0; i <= 5; i++) {
    solve(f[i], sz * 3);
    sz *= 10;
  }
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    cout << dp[n] << "\n";
  }
  return 0;
}
