//#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#include "boost/multiprecision/cpp_int.hpp"
typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
typedef pair<dd,dd> d_d;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(),v.end())
#define ERASE(v) v.erase(unique(v.begin(),v.end()),v.end())
#define POSL(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define POSU(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

void mod_print(ll k){
    ll P=1000;
    rep(y,1,P){
        ll x=mod(y*k);
        if(x+P>=i_7){
            x-=i_7;
        }
        if(abs(x)<=P){
            cout<<x<<"/"<<y<<endl;
            return;
        }
    }
    cout<<"nun"<<endl;
}
//////////////////////////


bool anspro(){
    ll n,a,b,c,d;cin>>n>>a>>b>>c>>d;
    ll N=500;
    ll dp[N];
    rep(i,0,N-1)dp[i]=inf;
    dp[0]=0;
    priority_queue<l_l,vector<l_l>,greater<l_l>>p;//cost,数
    
    p.push({0,0});
    while(!p.empty()){
        
        l_l top=p.top();p.pop();
        ll now=top.se;
        ll co=top.fi;
        if(now*2<=N-1){
            if(chmin(dp[now*2],co+a)){
                p.push({dp[now*2],now*2});
            }
        }
        if(now*3<=N-1){
            if(chmin(dp[now*3],co+b)){
                p.push({dp[now*3],now*3});
            }
        }
        if(now*5<=N-1){
            if(chmin(dp[now*5],co+c)){
                p.push({dp[now*5],now*5});
            }
        }
        if(now+1<=N-1){
            if(chmin(dp[now+1],co+d)){
                p.push({dp[now+1],now+1});
            }
        }
        if(now-1>=1){
            if(chmin(dp[now-1],co+d)){
                p.push({dp[now-1],now-1});
            }
        }
    }
    //rep(i,0,N-1)cout<<dp[i]<<" ";cout<<endl;
    
    priority_queue<l_l,vector<l_l>,greater<l_l>>q;//cost,数
    ll ans=inf;
    q.push({0,n});
    map<ll,ll>mp;
    while(!q.empty()){
        
        l_l top=q.top();q.pop();
        ll now=top.se;
        ll co=top.fi;
        if(mp[now]==0){
            mp[now]=co;
        }else{
            if(co>=mp[now])continue;
            else mp[now]=co;
        }
        
        ll nxt=now,cost=co;
        if(now<=N-1){
            chmin(ans,cost+dp[now]);
            continue;
        }
        
        while(1){
            if(nxt%2==0){
                q.push({cost+a,nxt/2});
                break;
            }else{
                cost+=d;
                nxt++;
            }
        }
        nxt=now;cost=co;
        while(1){
            if(nxt%2==0){
                q.push({cost+a,nxt/2});
                break;
            }else{
                cost+=d;
                nxt--;
            }
        }
        
        nxt=now;cost=co;
        while(1){
            if(nxt%3==0){
                q.push({cost+b,nxt/3});
                break;
            }else{
                cost+=d;
                nxt++;
            }
        }
        
        nxt=now;cost=co;
        while(1){
            if(nxt%3==0){
                q.push({cost+b,nxt/3});
                break;
            }else{
                cost+=d;
                nxt--;
            }
        }
        
        nxt=now;cost=co;
        while(1){
            if(nxt%5==0){
                q.push({cost+c,nxt/5});
                break;
            }else{
                cost+=d;
                nxt++;
            }
        }
        
        nxt=now;cost=co;
        while(1){
            if(nxt%5==0){
                q.push({cost+c,nxt/5});
                break;
            }else{
                cost+=d;
                nxt--;
            }
        }
        nxt=now;cost=co;
        ll t=nxt/4;
        LL T=t;
        LL D=d;
        if(T*D+cost<ans){
            q.push({cost+d*t,nxt-t});
        }
        
    }
    cout<<ans<<endl;
    
    return 0;
}

int main(){fastio
    
    inf=1E18;
    ll t;cin>>t;
    while(t--){
        anspro();
    }
    
    return 0;
}


Measured memory_bytes: 4888