#include <bits/stdc++.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
 
using namespace std;
 
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P, ll> T;
 
long long int INF = 2e18;
const ll fact_table = 1200008;
double Pi = 3.1415926535897932384626;

priority_queue <ll> pql;
priority_queue <P> pqp;
priority_queue <P> bag;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop
 
ll dx[8]={1,0,-1,0,1,1,-1,-1};
ll dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "DRUL";
//↓,→,↑,←

#define endl "\n"
#ifdef ENJAPMA
    #undef endl
#endif
#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(25)<<x<<endl;
#define pu(x) cout<<(x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define CLEAR(a) a = decltype(a)(); 
#define pc(x) cout << x << ",";
 
const ll mod = 998244353ll;
// const ll mod = 1000000007ll;
  
ll mypow(ll number1,ll number2, ll mod){
    if(number2 == 0){
        return 1ll;
    }else{
        ll number3 = mypow(number1,number2 / 2, mod);
        number3 *= number3;
        number3 %= mod;
        if(number2%2==1){
            number3 *= number1;
            number3 %= mod;
        }
        return number3;
    }
}
void YES(bool condition){
    if(condition){
        p("YES");
    }else{
        p("NO");
    }
    return;
}
void Yes(bool condition){
    if(condition){
        p("Yes");
    }else{
        p("No");
    }
    return;
}

/*
ll fact[fact_table + 5],rfact[fact_table + 5]; 
 
void c3_init(){
    fact[0] = rfact[0] = 1;
    for(ll i=1; i<=fact_table; i++){
        fact[i] = (fact[i-1]*i) % mod;
    }
    rfact[fact_table] = mypow(fact[fact_table],mod - 2, mod);
    for(ll i=fact_table; i>=1; i--){
       rfact[i-1] = rfact[i] * i;
       rfact[i-1] %= mod;
    }
    return;}
ll c3(ll n,ll r){
    return (((fact[n] * rfact[r]) % mod ) * rfact[n-r]) % mod;}
*/
bool multicase = true;

ll n,m,num,a,b,c,d,e,h,q;
ll w, k, ans;
ll x[500005], y[500005];
ll dp[15][500005];
string s;
ll query = 0;

ll res(ll n){
    if(2 <= n && n <= 100000 && dp[query][n] != 0){
        return dp[query][n];
    }
    if(n == 0){
        return 0ll;
    }else if(n == 1){
        return d;
    }else if(n < 0){
        return INF;
    }
    ll tmp = INF;
    ll two = n / 2;
    if(n != two)tmp = min(tmp, res(two) + a + d * abs(two * 2 - n));
    if(n != two + 1 && n % 2 != 0)tmp = min(tmp, res(two + 1) + a + d * abs(two * 2 + 2 - n));
    ll three = n / 3;
    if(n != three)tmp = min(tmp, res(three) + b + d * abs(three * 3 - n));
    if(n != three + 1 && n % 3 != 0)tmp = min(tmp, res(three + 1) + b + d * abs(three * 3 + 3 - n));
    ll five = n / 5;
    if(n != five)tmp = min(tmp, res(five) + c + d * abs(five * 5 - n));
    if(n != five + 1 && n % 5 != 0)tmp = min(tmp, res(five + 1) + c + d * abs(five * 5 + 5 - n));
    if(n <= 100000){
        dp[query][n] = tmp;
    }
    return tmp;
}

ll mul(ll a, ll b){
    if(a > INF / b){
        return INF;
    }else{
        return a * b;
    }
}

void solve(){
    query++;
    cin >> n;
    cin >> a >> b >> c >> d;
    while(!pqps.empty())pqps.pop();
    pqps.push(P(0, n));
    map<ll, ll> mp;
    mp[n] = 0;

    x[2] = c, x[1] = b, x[0] = a;
    y[2] = 5, y[1] = 3, y[0] = 2;
    ll ans = mul(n, d);
    while(!pqps.empty()){
        P p = pqps.top();
        pqps.pop();
        ll v = p.second;
        ll nowcost = p.first;
        if(nowcost > mp[v] && !(mp[v] == 0 && v != 0))continue;
        ll two = v / 2, three = v / 3, five = v / 5;
        vector<ll> nvs;
        nvs.pb(two); nvs.pb(two + 1); nvs.pb(three); nvs.pb(three + 1); nvs.pb(five); nvs.pb(five + 1);
        for(ll i=0;i<nvs.size();i++){
            ll nv = nvs[i];
            ll idx;
            if(i <= 5)idx = 2;
            if(i <= 3)idx = 1;
            if(i <= 1)idx = 0;
            ll newcost = mp[v] + x[idx] + d * abs(nv * y[idx] - v);
            if(mp[nv] > newcost || (mp[nv] == 0 && nv >= 1)){
                if(mul(nv, d) != INF)ans = min(ans, nv * d + newcost);
                mp[nv] = newcost;
                pqps.push(P(newcost, nv));
            }
        }   
    }
    p(ans);
    return;
}


int main(){
    // init();
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll q, testcase = 1;
    if(multicase){
        cin >> q;
    }else{
        q = 1; 
    }
    while(q--){
        // pu("Case ");pu("#");pu(testcase);pu(": ");
        solve();
        testcase++;
    }
    // solve();
    return 0;
}
















Measured memory_bytes: 4624