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


Measured memory_bytes: 16572