//AT AGC 044
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll A,B,C,D;
map<ll,ll>memo;
ll solve(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+solve(l2/2));
    res=min(res,llabs(r2-N)*D+A+solve(r2/2));
    res=min(res,llabs(l3-N)*D+B+solve(l3/3));
    res=min(res,llabs(r3-N)*D+B+solve(r3/3));
    res=min(res,llabs(l5-N)*D+C+solve(l5/5));
    res=min(res,llabs(r5-N)*D+C+solve(r5/5));
    memo[N]=res;
    return res;
}
int main(){
    int T;
    cin>>T;
    for(int t=0;t<T;t++){
        memo.clear();
        ll N;
        cin>>N;
        cin>>A>>B>>C>>D;
        cout<<solve(N)<<"\n";
    }
    return 0;
}