#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

void solve() {
  ll n; int a, b, c, d; cin >> n >> a >> b >> c >> d;
  map<ll, ll> dp;
  dp[n] = 0;
  using P = pair<ll, ll>;
  priority_queue<P, vector<P>, greater<P>> que;
  que.emplace(0, n);
  while (!que.empty()) {
    auto [coin, val] = que.top(); que.pop();
    if (dp[val] > coin) continue;
    if (val == 0) {
      cout << dp[val] << '\n';
      return;
    }
    if (log10(d) + log10(val) < 18 + EPS && (dp.count(0) == 0 || dp[0] > coin + val * d)) {
      dp[0] = coin + val * d;
      que.emplace(dp[0], 0);
    }
    if (val % 2 == 0 && (dp.count(val / 2) == 0 || dp[val / 2] > coin + a)) {
      dp[val / 2] = coin + a;
      que.emplace(dp[val / 2], val / 2);
    }
    if (val % 3 == 0 && (dp.count(val / 3) == 0 || dp[val / 3] > coin + b)) {
      dp[val / 3] = coin + b;
      que.emplace(dp[val / 3], val / 3);
    }
    if (val % 5 == 0 && (dp.count(val / 5) == 0 || dp[val / 5] > coin + c)) {
      dp[val / 5] = coin + c;
      que.emplace(dp[val / 5], val / 5);
    }
    {
      ll nx = val / 2 * 2;
      if (nx < val && (dp.count(nx / 2) == 0 || dp[nx / 2] > coin + (val - nx) * d + a)) {
        dp[nx / 2] = coin + (val - nx) * d + a;
        que.emplace(dp[nx / 2], nx / 2);
      }
    }
    {
      ll nx = (val + 1) / 2 * 2;
      if (val < nx && (dp.count(nx / 2) == 0 || dp[nx / 2] > coin + (nx - val) * d + a)) {
        dp[nx / 2] = coin + (nx - val) * d + a;
        que.emplace(dp[nx / 2], nx / 2);
      }
    }
    {
      ll nx = val / 3 * 3;
      if (nx < val && (dp.count(nx / 3) == 0 || dp[nx / 3] > coin + (val - nx) * d + b)) {
        dp[nx / 3] = coin + (val - nx) * d + b;
        que.emplace(dp[nx / 3], nx / 3);
      }
    }
    {
      ll nx = (val + 2) / 3 * 3;
      if (val < nx && (dp.count(nx / 3) == 0 || dp[nx / 3] > coin + (nx - val) * d + b)) {
        dp[nx / 3] = coin + (nx - val) * d + b;
        que.emplace(dp[nx / 3], nx / 3);
      }
    }
    {
      ll nx = val / 5 * 5;
      if (nx < val && (dp.count(nx / 5) == 0 || dp[nx / 5] > coin + (val - nx) * d + c)) {
        dp[nx / 5] = coin + (val - nx) * d + c;
        que.emplace(dp[nx / 5], nx / 5);
      }
    }
    {
      ll nx = (val + 4) / 5 * 5;
      if (val < nx && (dp.count(nx / 5) == 0 || dp[nx / 5] > coin + (nx - val) * d + c)) {
        dp[nx / 5] = coin + (nx - val) * d + c;
        que.emplace(dp[nx / 5], nx / 5);
      }
    }
  }
}

int main() {
  int t; cin >> t;
  while (t--) solve();
  return 0;
}


Measured memory_bytes: 4588