#include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\n'

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;

constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
bool chmax(T& a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T& a, T b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

struct node{
    lint id;
    lint cost;

    bool operator<(const node& rhs) const {
        return cost > rhs.cost;
    }
};

void solve(void){
    lint n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;

    map<lint, lint> cost;
    cost[n] = 0;
    priority_queue<node> que;
    que.push({n, 0LL});

    while(!que.empty()){
        node now = que.top();
        que.pop();

        if(cost[now.id] < now.cost){
            continue;
        }

        if(now.id % 2LL == 0LL){
            auto it2 = cost.find(now.id / 2LL);
            lint nc = a;
            if(now.id - now.id / 2LL <= LLONG_MAX / d){
                chmin(nc, (now.id - now.id / 2LL) * d);
            }
            if(it2 == cost.end() || (*it2).second > now.cost + nc){
                cost[now.id / 2LL] = now.cost + nc;
                que.push({now.id / 2LL, now.cost + nc});
            }
        }

        if(now.id % 3LL == 0LL){
            auto it3 = cost.find(now.id / 3LL);
            lint nc = b;
            if(now.id - now.id / 3LL <= LLONG_MAX / d){
                chmin(nc, (now.id - now.id / 3LL) * d);
            }
            if(it3 == cost.end() || (*it3).second > now.cost + nc){
                cost[now.id / 3LL] = now.cost + nc;
                que.push({now.id / 3LL, now.cost + nc});
            }
        }

        if(now.id % 5LL == 0LL){
            auto it5 = cost.find(now.id / 5LL);
            lint nc = c;
            if(now.id - now.id / 5LL <= LLONG_MAX / d){
                chmin(nc, (now.id - now.id / 5LL) * d);
            }
            if(it5 == cost.end() || (*it5).second > now.cost + nc){
                cost[now.id / 5LL] = now.cost + nc;
                que.push({now.id / 5LL, now.cost + nc});
            }
        }

        if(now.id % 30LL != 0LL){
            auto itp1 = cost.find(now.id + 1LL);
            if(itp1 == cost.end() || (*itp1).second > now.cost + d){
                cost[now.id + 1LL] = now.cost + d;
                que.push({now.id + 1LL, now.cost + d});
            }

            auto itm1 = cost.find(now.id - 1LL);
            if(itm1 == cost.end() || (*itm1).second > now.cost + d){
                cost[now.id - 1LL] = now.cost + d;
                que.push({now.id - 1LL, now.cost + d});
            }
        }
    }

    cout << cost[0] << '\n';

    return;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int T;
    cin >> T;

    while(T--){
        solve();
    }

    cout << flush;

    return 0;
}

Measured memory_bytes: 14424