#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

constexpr double EPS = 1e-10;
//constexpr double PI  = acos(-1.0);
//constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;
//inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template<class T> inline T sqr(T x) {return x*x;}

void solve() {
    int t;
    cin >> t;
    LL n,a,b,c,d;
    REP(ttt,0,t) {
        cin >> n >> a >> b >> c >> d;
        map<LL,LL> m;
        priority_queue<pair<LL,LL>, vector<pair<LL,LL>>, greater<pair<LL,LL>>> q;
        q.emplace(0,n);
        while (!q.empty()) {
            auto p = q.top();
            q.pop();
            LL sum = p.first, v = p.second;
            if (v == 0) {
                cout << sum << endl;
                break;
            }
            if (m.count(v) > 0 && m[v] <= sum) continue;
            m[v] = sum;
            //cerr << v << ", " << sum << endl;

            if (v == 1) {
                q.emplace(sum + d, 0);
                continue;
            }

            //LL maxn = n + 1'000'000'000;
            //LL maxn = n + (1LL<<60) + 1;
            LL maxn = n * 100;

            LL sr = (1LL<<60) / d;
            if (v % 2 == 0) {
                if (v /2 <= sr && a > v / 2 * d) q.emplace(sum + v / 2 * d, v / 2); 
                else q.emplace(sum + a, v / 2);
            }
            else {
                q.emplace(sum + d, v - 1);
                if (v + 1 < maxn) q.emplace(sum + d, v + 1);
            }
            if (v % 3 == 0) {
                if (v / 3 * 2 <= sr && b > v / 3 * d * 2) q.emplace(sum + v / 3 * d * 2, v / 3);
                else q.emplace(sum + b, v / 3);
            }
            else {
                q.emplace(sum + d * (v % 3), v - v % 3);
                if (v + (3 - v % 3) < maxn) q.emplace(sum + d * (3 - (v % 3)), v + (3 - v % 3));
            }
            if (v % 5 == 0) {
                if (v / 5 * 4 <= sr && c > v / 5 * d * 4) q.emplace(sum + v / 5 * d * 4, v / 5);
                else q.emplace(sum + c, v / 5);
            }
            else {
                q.emplace(sum + d * (v % 5), v - v % 5);
                if (v + (5 - v % 5) < maxn) q.emplace(sum + d * (5 - (v % 5)), v + (5 - v % 5));
            }
            /*
            if (v * 2 <= n * 2) q.emplace(sum + a, v * 2);
            if (v * 3 <= n * 2) q.emplace(sum + b, v * 3);
            if (v * 5 <= n * 2) q.emplace(sum + c, v * 5);
            if (v + 1 <= n * 2) q.emplace(sum + d, v + 1);
            if (v - 1 >= 0) q.emplace(sum + d, v - 1);
            */
        }
    }
}

signed main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 7516