Problem:
Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

Multiply the number by 2, paying A coins.

Multiply the number by 3, paying B coins.

Multiply the number by 5, paying C coins.

Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

1 \le T \le 10

1 \le N \le 10^{18}

1 \le A, B, C, D \le 10^9

All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases.
Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.

Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 1 to multiply by 2 (x = 4).

Pay 2 to multiply by 3 (x = 12).

Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 2 to multiply by 5 (x = 10).

Pay 8 to increase by 1 (x = 11).

Solution in C++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(long long i = 0;i < n;++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+7  
using ll = long long; 
using P=pair<int,int>;
using lP=pair<ll,ll>;
using fP=pair<double,double>;
ll const mod=1e9+7;
const ll MAX=300000;
using vi=vector<int>;
using vc=vector<char>;
using vs=vector<string>;
using vvi =vector<vector<int>>;
using vvc=vector<vector<char>>;
using vvp =vector<vector<P>>;


struct edge{int to,cost;};
int main(){
    int t;
    cin>>t;
    
    rep(i,t){
        ll n,a,b,c,d;
        cin>>n>>a>>b>>c>>d;
        priority_queue<lP,vector<lP>,greater<lP>> q;


        map<ll,int> mp;
        q.push(P(0,n));
        int cnt=0;
        while(q.size()){
            lP p=q.top();q.pop();
            ll v=p.second;
            if(p.second==1)continue;
            if(mp[v]>0&&mp[v]<p.first)continue;
            
            if(cnt<6&&mp[v+1]==0||mp[v+1]>p.first+d){
                cnt++;
                mp[v+1]=p.first+d;
                q.push(lP(p.first+d,v+1));
            }
            if(mp[v-1]==0||mp[v-1]>p.first+d){
                mp[v-1]=p.first+d;
                q.push(lP(p.first+d,v-1));
            }
            if(v%2==0){
                if(mp[v/2]==0||mp[v/2]>p.first+a){
                mp[v/2]=p.first+a;
                q.push(lP(p.first+a,v/2));
                }
            }
            if(v%3==0){
                if(mp[v/3]==0||mp[v/3]>p.first+b){
                mp[v/3]=p.first+b;
                q.push(lP(p.first+b,v/3));
                }
            }
            if(v%5==0){
                if(mp[v/5]==0||mp[v/5]>p.first+c){
                mp[v/5]=p.first+c;
                q.push(lP(p.first+c,v/5));
                }
            }
        }
       cout<<mp[1]+d<<endl;
    }
    
    return 0;
}