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>
#include <boost/multiprecision/cpp_int.hpp>
#define int long long
#define ll long long
#define Cint boost::multiprecision::cpp_int
using ull = unsigned long long;
using namespace std;
#define dump(x)                             \
    if (dbg) {                              \
        cerr << #x << " = " << (x) << endl; \
    }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
/*
template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}
*/
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    } else
        return 0;
}
void Yes(bool flag = true) {
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
void No(bool flag = true) {
    Yes(!flag);
}
void YES(bool flag = true) {
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
void NO(bool flag = true) {
    YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = false;
}  // namespace mydef
using namespace mydef;
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans

Cint T;

Cint N_MAX = 1000000000000000010ll;
Cint memo[70][70][70];
//map<Cint, Cint> g[70][70][70];
map<Cint, Cint> g;
Cint N, A, B, C, D;

bool chmin(Cint& a, Cint b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}


Cint gosa(Cint G) {
    if (g.count(G)) {
        return g[G];
    }
    if (G == 1)
        return D;
    if (G == 0) {
        return N * D;
    }

    Cint tmp = G * D;
    chmin(tmp, gosa(G / 2) + A + (G % 2) * D);
    chmin(tmp, gosa(G / 3) + B + (G % 3) * D);
    chmin(tmp, gosa(G / 5) + C + (G % 5) * D);

    if (G > G / 2 + 1)
        chmin(tmp, gosa(G / 2 + 1) + A + D * (2 - G % 2));
    if (G > G / 3 + 1)
        chmin(tmp, gosa(G / 3 + 1) + B + D * (3 - G % 3));
    if (G > G / 5 + 1)
        chmin(tmp, gosa(G / 5 + 1) + C + D * (5 - G % 5));

    return g[G] = tmp;
}


/*
Cint gosa(Cint G, Cint a, Cint b, Cint c) {
    if (g[G][a][b][c].count()) {
        return g[G][a][b][c];
    }
    if (G == 0)
        return 1;

    Cint tmp = G;
    chmin(tmp, gosa(G / 2) + 1 + G % 2);
    chmin(tmp, gosa(G / 3) + 1 + G % 3);
    chmin(tmp, gosa(G / 5) + 1 + G % 5);

    if (G > G / 2 + 1)
        chmin(tmp, gosa(G / 2 + 1) + 1 + (2 - G % 2));
    if (G > G / 3 + 1)
        chmin(tmp, gosa(G / 3 + 1) + 1 + (3 - G % 3));
    if (G > G / 5 + 1)
        chmin(tmp, gosa(G / 5 + 1) + 1 + (5 - G % 5));
}
*/


void solve() {
    N_MAX *= 10;
    Fill(memo, N_MAX);
    for (int i = 0; i < 63; i++) {
        memo[i][0][0] = 1ll << i;
        if (memo[i][0][0] < N_MAX) {
            for (int j = 0; j < 60; j++) {
                if (memo[i][j][0] < N_MAX) {
                    memo[i][j + 1][0] = memo[i][j][0] * 3;
                    for (int k = 0; k < 30; k++) {
                        if (memo[i][j][k] < N_MAX) {
                            memo[i][j][k + 1] = memo[i][j][k] * 5;
                        } else
                            break;
                    }
                } else
                    break;
            }
        } else
            break;
    }

    while (T--) {
        cin >> N >> A >> B >> C >> D;
        g.clear();

        cout << gosa(N) << endl;

        /*
        for (int i = 0; i < 63; i++) {
            if (memo[i][0][0] < N_MAX) {
                for (int j = 0; j < 60; j++) {
                    if (memo[i][j][0] < N_MAX) {
                        for (int k = 0; k < 30; k++) {
                            if (memo[i][j][k] < N_MAX) {
                                //chmin(ans, i * A + j * B + k * C + D * abs(memo[i][j][k] - N));
                                if (ans > (int)(i * A + j * B + k * C + D * (abs(memo[i][j][k] - N)))) {
                                    ans = min(ans, (int)(i * A + j * B + k * C + D * gosa(abs(memo[i][j][k] - N)), i, j, k));
                                    cerr << i << " " << j << " " << k << " " << ans << endl;
                                }
                            } else
                                break;
                        }
                    } else
                        break;
                }
            } else
                break;
        }
        cout << ans + D << endl;
        */
    }
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);


    cin >> T;
    solve();
    return 0;
}