#include <bits/stdc++.h>
 
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
 
const int MOD = 1e9+7;
// const int MOD  = 998244353;
const int INF = 0x3f3f3f3f;
const double PI = 4*atan(1);
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
// struct custom_hash {
//     static uint64_t splitmix64(uint64_t x) {
//         // http://xorshift.di.unimi.it/splitmix64.c
//         x += 0x9e3779b97f4a7c15;
//         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//         return x ^ (x >> 31);
//     }
 
//     size_t operator()(uint64_t x) const {
//         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
//         return splitmix64(x + FIXED_RANDOM);
//     }
// };
// ll gcd(ll a, ll b) { 
//     if (a == 0) return b; 
//     return gcd(b % a, a); 
// }
// ll mod(ll x) {
//  x%=MOD;
//  if(x<0)x+=MOD;
//  return x;
// }
// ll power(int base, int power) {
//  ll prod = 1;
//  ll b = base;
//  while(power) {
//      if (power & 1) {
//          prod = mod(prod*b);
//      }
//      b = mod(b*b);
//      power/=2;
//  }
//  return prod;
// }
// struct line {
//     int m, c;
//     ll eval(int x) { return 1LL * m * x + c; }
//     double intersectX(line l) { return (double) (c - l.c) / (l.m - m); }
// double degToRad(double x) {
//  return x * PI / 180;
// }
// double area(int a, int b, int c, int d, int e, int f) {
//  return abs(0.5*(a*(d-f)+c*(f-b)+e*(b-d)));
// }
ll n,a,b,c,d;
map<ll,ll> memo;
// ll f(ll x) {
//     if (x==0) return 0;
//     if (x==1) return d;
//     if (memo.find(x) != memo.end()) return memo[x];
//     ll res = 1e18;
//     if (n<res/d) res = n*d;
//     ll c2 = ((x+1)/2)*2, f2 = (x/2)*2;
//     ll c3 = ((x+2)/3)*3, f3 = (x/3)*3;
//     ll c5 = ((x+4)/5)*5, f5 = (x/5)*5;
//     res = min(res,a+min(f(c2/2)+d*(c2-x),f(f2/2)+d*(x-f2)));
//     res = min(res,b+min(f(c3/3)+d*(c3-x),f(f3/3)+d*(x-f3)));
//     res = min(res,c+min(f(c5/5)+d*(c5-x),f(f5/5)+d*(x-f5)));
//     return memo[x] = res;
// }
ll f(ll N) {
    if (N == 0) return 0;
    if (N == 1) return d;
    if (memo.count(N)) return memo[N];
    ll l2 = (N/2)*2;
    ll r2 = ((N+1)/2)*2;
    ll l3 = (N/3)*3;
    ll r3 = ((N+2)/3)*3;
    ll l5 = (N/5)*5;
    ll r5 = ((N+4)/5)*5;
 
    ll res = 1e18;
    if (N < res/d) res = N*d;
    res = min(res, llabs(l2-N)*d + a + f(l2/2));
    res = min(res, llabs(r2-N)*d + a + f(r2/2));
 
    res = min(res, llabs(l3-N)*d + b + f(l3/3));
    res = min(res, llabs(r3-N)*d + b + f(r3/3));
 
    res = min(res, llabs(l5-N)*d + c + f(l5/5));
    res = min(res, llabs(r5-N)*d + c + f(r5/5));
 
    memo[N] = res;
 
    return res;
}
void solve() {
    memo.clear();
    cin >> n >> a >> b >> c >> d;
    cout << f(n) << endl;
}
int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int t=1;
    cin >> t;
    for (int i = 1; i <= t; i++) { 
        // cout << "Case #" << i << " :";
        solve();
    }
}

Measured memory_bytes: 4440