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: Accepted)

#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();
    }
}