#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#include <functional>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

ll A, B, C, D;
ll ans;
map<ll, ll> mp;

void solve(ll N, ll tmp) {
    if (tmp < 0) {
        return;
    }
    if (mp.count(N) > 0) {
        if (mp[N] <= tmp) {
            return;
        } else {
            mp[N] = tmp;
        }
    } else {
        mp[N] = tmp;
    }
    // cout << N << " " << tmp << endl;
    if (tmp >= ans) {
        return;
    }
    if (N == 1) {
        // cout << tmp << endl;
        if (D + tmp > 0) {
            ans = min(ans, D + tmp);
        }
        return;
    }
    if (N <= inf / D && N * D <= inf - tmp) {
        ans = min(ans, N * D + tmp);
    }
    if (N % 2 == 0) {
        solve(N / 2, tmp + A);
    } else {
        solve((N - 1) / 2, tmp + A + D);
        solve((N + 1) / 2, tmp + A + D);
    }
    if (N % 3 == 0) {
        solve(N / 3, tmp + B);
    } else if (N % 3 == 1) {
        solve((N - 1) / 3, tmp + B + D);
        // solve((N + 2) / 3, tmp + B + 2 * D);
    } else {
        solve((N + 1) / 3, tmp + B + D);
        // if (N > 2) {
        //     solve((N - 2) / 3, tmp + B + 2 * D);
        // }
    }
    if (N % 5 == 0) {
        solve(N / 5, tmp + C);
    } else if (N % 5 == 1) {
        solve((N - 1) / 5, tmp + C + D);
        // solve((N + 4) / 5, tmp + C + 4 * D);
    } else if (N % 5 == 2) {
        if (N > 2) {
            solve((N - 2) / 5, tmp + C + 2 * D);
        } else {
            solve((N + 3) / 5, tmp + C + 3 * D);
        }
    } else if (N % 5 == 3) {
        // if (N > 3) {
        //     solve((N - 3) / 5, tmp + C + 3 * D);
        // }
        solve((N + 2) / 5, tmp + C + 2 * D);
    } else {
        // if (N > 4) {
        //     solve((N - 4) / 5, tmp + C + 4 * D);
        // }
        solve((N + 1) / 5, tmp + C + D);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll T;
    cin >> T;
    REP(i, 0, T) {
        ll NN;
        cin >> NN >> A >> B >> C >> D;
        ans = inf;
        mp.clear();
        solve(NN, 0);
        cout << ans << endl;
    }
}


Measured memory_bytes: 4292