#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;


lint N, A, B, C, D;
std::string ll2bitstr(long long x)
{
    std::string ret;
    while (x)
    {
        ret += x % 2 + '0', x /= 2;
    }
    std::reverse(ret.begin(), ret.end());
    return ret;
}

__int128 solve2(lint x)
{
    string s = ll2bitstr(x);
    int N = s.length() + 1;

    vector<lint> dp0(N, 1e12);
    vector<lint> dpk(N, 1e12);
    vector<lint> dpyo(N, 1e12);
    dp0[0] = 0;
    dp0[1] = D + A * (s.length() - 1);
    dpyo[0] = D + A * s.length();
    REP(i, s.length())
    {
        if (s[i] == '0')
        {
            dp0[i + 1] = min(dp0[i], dpk[i]);
            dpyo[i + 1] = min({dp0[i] + D, dpk[i] + D});
        }
        else
        {
            if (i != 0) dp0[i + 1] = min({dp0[i] + D, dpk[i] + D, dpyo[i] + D});
            dpk[i + 1] = min(dpk[i], dpyo[i] + D);
        }
    }
    lint ret = min(dp0.back(), dpk.back());
    if (__int128(x) * D < ret) ret = x * D;
    // dbg(x);
    // dbg(s);
    // dbg(dp0);
    // dbg(dpk);
    // dbg(dpyo);
    // dbg(ret);
    return ret;
}
map<pair<lint, bool>, lint> mp;

__int128 rec(lint x, bool ok_add)
{
    if (x == 0) return 0;
    if (x == 1) return D;
    if (mp.count(make_pair(x, ok_add))) return mp[make_pair(x, ok_add)];

    __int128 ret = __int128(D) * x;

    if (x % 2)
    {
        if (ok_add) chmin(ret, rec(x / 2 + 1, false) + A);
        else chmin(ret, rec(x / 2, true) + A + D);
    }
    else
    {
        chmin(ret, rec(x / 2, false) + A);
    }

    int mod3 = x % 3;
    if (mod3 == 1 and ok_add)
    {
        chmin(ret, rec(x / 3, true) + B + D);
    }
    else if (mod3 <= 1)
    {
        chmin(ret, rec(x / 3, false) + B + D * mod3);
    }
    else
    {
        chmin(ret, rec(x / 3 + 1, false) + B + D * (3 - mod3 - ok_add));
    }
    int mod5 = x % 5;
    if (mod5 == 2 and ok_add)
    {
        chmin(ret, rec(x / 5, true) + C + D * mod5);
    }
    else if (mod5 <= 2)
    {
        chmin(ret, rec(x / 5, false) + C + D * mod5);
    }
    else
    {
        chmin(ret, rec(x / 5 + 1, false) + C + D * (5 - mod5 - ok_add));
    }

    mp[make_pair(x, ok_add)] = ret;
    return ret;
}

lint Sol()
{
    mp.clear();
    cin >> N >> A >> B >> C >> D;
    return rec(N, false);
}

int main()
{
    int T;
    cin >> T;
    while (T--) cout << Sol() << '\n';
}


Measured memory_bytes: 4252