/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = [](){ ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(17); return 0; }();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename ...T> string format(const string& fmt, T&&... args) {
    size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
    unique_ptr<char[]> buf(new char[sz]);
    snprintf(buf.get(), sz, fmt.c_str(), args...);
    return string(buf.get(), buf.get() + sz - 1);
}

template<class T> struct rge { T b, e; auto begin() const { return b; } auto end() const { return e; } };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template<class T> auto dud(T* x) -> decltype(cerr << *x, 0);
template<class T> char dud(...);

struct debug {
#ifdef LOCAL
    debug(int line) {
        if(line) cerr << "LINE(" << line << ") -> ";
    }
    template<class T> typename enable_if<sizeof dud<T>(0) != 1, debug&>::type operator<<(T i) {
        cerr << boolalpha << i; return * this;
    }
    template<class T> typename enable_if<sizeof dud<T>(0) == 1, debug&>::type operator<<(T i) {
        return *this << range(begin(i), end(i));
    }
    template<class T, class U> debug& operator<<(pair<T, U> d) {
        return *this << "(" << d.first << ", " << d.second << ")";
    }
    debug& operator<<(tuple<>&) { return *this << "()"; };
    template<class ...T> debug& operator<<(tuple<T...> d) {
        *this << "("; debug_tuple<sizeof...(T), 0>(d);
        return *this << ")";
    }
    template<size_t L, size_t I, class T> void debug_tuple(const T& t) {
        *this << (I == 0 ? "" : ", ") << get<I>(t);
        if(I + 1 < L) debug_tuple<L, (I + 1) % L>(t);
    }
    template<class T> debug & operator <<(rge<T> d) {
        *this << "[";
        for(auto it = d.b; it != d.e; ++it)
            *this << (it != d.b ?  ", " : "") << *it;
        return *this << "]";
    }
    debug& operator<<(ostream&(*pf)(std::ostream&)) {
        cerr << pf; return *this;
    }
#else
    debug(int) {}
    template<class T> debug& operator<<(T&&) { return *this; }
    debug& operator<<(ostream&(*)(std::ostream&)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0()  debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 200005;

struct defaultdict {
    unordered_map<LL, LL> dict;
    LL& operator[](LL key) {
        if(dict.find(key) == dict.end()) {
            dict[key] = LLINF;
        }
        return dict[key];
    }
};

void solve(int) {
    using P = pair<LL, LL>;
    LL N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    priority_queue<P, vector<P>, greater<P>> pq;
    defaultdict dist;
    dist[N] = 0;
    pq.push({0, N});

    auto calc_cost = [](auto X, auto D, auto diff) {
        auto res = X;
        if(diff <= LLONG_MAX / D) {
            res = min(res, D * diff);
        }
        return res;
    };

    LL ans = -1;
    while(pq.size()) {
        LL cost, n;
        tie(cost, n) = pq.top();
        pq.pop();

        if(n == 0) {
            if(ans == -1 or ans > cost) {
                ans = cost;
            }
            break;
        }

        if(cost > dist[n]) {
            continue;
        }

        for(int i = -4; i <= 4 and n >= i; ++i) {
            auto m = n - i;
            if(abs(i) < 2 and m % 2 == 0) {
                auto cost1 = calc_cost(A, D, m - m / 2) + abs(i) * D;

                if(dist[m / 2] > cost + cost1) {
                    dist[m / 2] = cost + cost1;
                    pq.push({cost + cost1, m / 2});
                }
            }

            if(abs(i) < 3 and m % 3 == 0) {
                auto cost1 = calc_cost(B, D, m - m / 3) + abs(i) * D;

                if(dist[m / 3] > cost + cost1) {
                    dist[m / 3] = cost + cost1;
                    pq.push({cost + cost1, m / 3});
                }
            }

            if(m % 5 == 0) {
                auto cost1 = calc_cost(C, D, m - m / 5) + abs(i) * D;

                if(dist[m / 5] > cost + cost1) {
                    dist[m / 5] = cost + cost1;
                    pq.push({cost + cost1, m / 5});
                }
            }
        }

        if(n <= LLONG_MAX / D and dist[0] > cost + n * D) {
            dist[0] = cost + n * D;
            pq.push({cost + n * D, 0});
        }

    }
    assert(ans != -1);
    cout << ans << endl;
}

int main() {
    int T = 1;
    cin >> T;
    for(int i = 1; i <= T; ++i) {
        solve(i);
    }
    return 0;
}


Measured memory_bytes: 4224