#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
long long int k, n;
long long int f[6];
long long int cost(long long int x) {
  int cnt = 0;
  long long int ans = 0;
  while (x) {
    if ((x % 10) % 3 == 0) {
      ans += (x % 10) / 3 * f[cnt];
    }
    cnt++;
    x /= 10;
  }
  return ans;
}
int main() {
  cin >> k;
  vector<long long int> p(6);
  p[0] = 1;
  for (int i = 1; i < 6; i++) p[i] = p[i - 1] * 10;
  for (int i = 0; i < 6; i++) cin >> f[i];
  int q;
  cin >> q;
  while (q--) {
    cin >> n;
    vector<long long int> v, w;
    vector<long long int> dp(n + 1, -(long long int)(1e18));
    dp[0] = 0;
    for (int i = 0; i < 6; i++) {
      long long int cur = 0;
      long long int z = 1;
      while (cur + z <= 3 * (k - 1)) {
        cur += z;
        v.push_back(f[i] * z);
        w.push_back(3 * z * p[i]);
        z *= 2;
      }
      if (3 * (k - 1) > cur) {
        z = 3 * (k - 1) - cur;
        v.push_back(f[i] * z);
        w.push_back(3 * z * p[i]);
      }
    }
    long long int ans = 0;
    for (int i = 0; i < (int)(w.size()); i++) {
      for (int j = n; j - w[i] >= 0; j--) {
        if (dp[j - w[i]] != -(long long int)(1e18)) {
          dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
        }
      }
    }
    for (int i = 0; i <= n; i++) {
      ans = max(ans, dp[i] + cost(n - i));
    }
    cout << ans << '\n';
  }
}
