#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937_64 mt(736736);
void remax(ll &x, const ll y) { x = max(x, y); }
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int k;
  cin >> k;
  vector<ll> f(6);
  for (auto &it : f) cin >> it;
  int q;
  cin >> q;
  vector<int> qs(q);
  for (auto &it : qs) cin >> it;
  vector<ll> digit_cost(10, 0);
  for (int i = 3; i < int(digit_cost.size()); i += 3) digit_cost[i] = i / 3;
  vector<int> pws(f.size(), 1);
  for (int i = 1; i < int(pws.size()); i++) pws[i] = pws[i - 1] * 10;
  vector<ll> dp(int(1e6), numeric_limits<ll>::min());
  dp[0] = 0;
  for (int d = 0; d < int(f.size()); d++) {
    int remain = 3 * (k - 1);
    vector<int> blocks;
    for (int block = 1; remain > 0; block *= 2) {
      const int cur = min(remain, block);
      blocks.push_back(cur);
      remain -= cur;
    }
    assert(remain == 0);
    for (const ll block : blocks) {
      const ll cost = f[d] * block;
      const ll weight = 3LL * block * pws[d];
      if (weight <= int(dp.size()))
        for (int i = int(dp.size()) - 1 - int(weight); i >= 0; i--)
          remax(dp[i + weight], dp[i] + cost);
    }
  }
  vector<ll> next = dp;
  for (int d = 0; d < int(f.size()); d++) {
    copy(dp.begin(), dp.end(), next.begin());
    for (int digit = 1; digit <= 9; digit++)
      for (int i = 0; i + digit * pws[d] < int(next.size()); i++)
        remax(next[i + digit * pws[d]], dp[i] + digit_cost[digit] * f[d]);
    copy(next.begin(), next.end(), dp.begin());
  }
  for (const auto it : qs) cout << dp[it] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}
