Problem:
Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

Multiply the number by 2, paying A coins.

Multiply the number by 3, paying B coins.

Multiply the number by 5, paying C coins.

Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

1 \le T \le 10

1 \le N \le 10^{18}

1 \le A, B, C, D \le 10^9

All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases.
Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.

Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 1 to multiply by 2 (x = 4).

Pay 2 to multiply by 3 (x = 12).

Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 2 to multiply by 5 (x = 10).

Pay 8 to increase by 1 (x = 11).

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include<algorithm>
#include<string>
#include <cmath>
#include <vector>
#include <map>
#include <cstdio>
#include <iomanip>
#include<set>
#include <numeric>
#include <queue>

#define int long long
#define double long double
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
const  int INF = 1e12;
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
const int ddx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
const int ddy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
const int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const int umonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
#define krep(i,k,n) for(int i=(k);i<n+k;i++)
#define Krep(i,k,n) for(int i=(k);i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define Rrep(i,n) for(int i=n;i>0;i--)
#define REP(i,n) for(int i=1;i<=n;i++)
#define LAST(x) x[x.size()-1]
#define ALL(x) (x).begin(),(x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x)
#define swap(a,b) (a += b,b = a - b,a -= b)
#define DIVCEIL(a,b) ((a+b-1)/b)
#define SUM1n(n) ((n)*(n+1)/2)
#define SUMkn(k,n) (SUM1n(n)-SUM1n(k-1))
#define PB push_back
#define Fi first
#define Se second 

int intpow(int a, int n) {
	// a^nのint ver
	if (n == 0)
		return 1;
	else {
		rep(i, n - 1)
			a *= a;
		return a;
	}
}

int FACT(int a) {
	if (a == 0)
		return 1;
	else
		return a * FACT(a - 1);
}

int nPr(int n, int r) {
	int s = n - r + 1;
	int sum = 1;
	for (int i = s; i <= n; i++)
		sum *= i;
	return sum;
}
int MODnPr(int n, int r, int mod) {
	int s = n - r + 1;
	int sum = 1;
	for (int i = s; i <= n; i++) {
		sum *= i;
		sum = sum % MOD;
	}
	return sum;
}
//int nCr(int n, int r)
int nCr2(int n, int r) {
	return FACT(n) / (FACT(r) * FACT(n - r));
}
int GCD(int a, int b)
{
	if (a < b)
		swap(a, b);
	if (b == 0)
		return a;
	if (a % b == 0)
		return b;
	return GCD(b, a % b);
}
int LCM(int a, int b)
{
	return  a * b / GCD(a, b);
}
int NUMOFDIV(int n) {
	//約数の数（だっけ？）
	int ans = 0;
	REP(i, n) {
		if (n % i == 0)
			ans++;
	}
	return ans;
}
int CEIL1(int n) {
	//1桁目切り上げ
	return (n + 9) / 10 * 10;
}
int GetDigit(int n) {
	return log10(n) + 1;
}
int DIGIT(int n, int k) {
	//nのk桁目
	rep(i, k - 1)
		n /= 10;
	return n % 10;
}
int DIGITSUM(int n) {
	int sum = 0, dig;
	while (n) {
		dig = n % 10;
		sum += dig;
		n /= 10;
	}
	return sum;
}
int DIVTIME(int n, int k) {
	//nをkで何回割れるか的な
	int div = 0;
	while (n % k == 0) {
		div++;
		n /= k;
	}
	return div;
}
int binary(int n) {
	//10進数→2進数
	int ans = 0;
	for (int i = 0; n > 0; i++) {
		ans += n % 2 * intpow(10, i);
		n /= 2;
	}
	return ans;
}
int intabs(int n) {
	if (n < 0)
		return -1 * n;
	else
		return n;
}
double LOG(int a, int b) {
	return log(b) / log(a);
}
double DISTANCE(int x1, int y1, int x2, int y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
double WatchAngle(int h, int m) {
	h %= 12;
	double mm = 6.0 * m;
	double nn = 30.0 * h + 0.5 * m;
	return std::min(fabs(mm - nn), 360.0 - fabs(nn - mm));
}
inline bool BETWEEN(int x, int min, int max) {
	if (min <= x && x <= max)
		return true;
	else
		return false;
}
inline bool between(int x, int min, int max) {
	if (min < x && x < max)
		return true;
	else
		return false;
}
inline bool isPRIME(int x) {
	if (x == 1)
		return false;
	if (x == 2)
		return true;
	if (x % 2 == 0)
		return false;
	double sqrtx = sqrt(x);
	for (int i = 3; i <= sqrtx; i += 2) {
		if (x % i == 0)
			return false;
	}
	return true;
}
inline bool isSQRT(int n) {
	if (sqrt(n) == (int)sqrt(n))
		return true;
	else
		return false;
}
inline bool isURU(int y) {
	if (y % 400 == 0)
		return 1;
	if (y % 100 == 0)
		return 0;
	if (y % 4 == 0)
		return 1;
	return 0;
}
// 順位付け
using namespace std;

using Graph = vector<vector<int>>;
int n, A[3], D, p[3] = { 2,3,5 };
map <int, int>mp;

int dfs(int N) {
	cout << N << endl;
	if (mp.count(N))
		return mp[N];
	if (N == 0)
		return mp[0] = 0;
	if (N == 1)
		return mp[1] = D;

	int ret = INF;
	if (N <= n / D)
		ret = min(ret, N * D);
	rep(i,3){
		ret = min(ret, dfs(N / p[i]) + A[i] + (N % p[i]) * D);
		ret = min(ret, dfs(DIVCEIL(N,p[i])) + A[i] + (p[i] - N % p[i]) * D);
	}
	return mp[N] = ret;
}


signed main() {

	ios::sync_with_stdio(0);
	cin.tie(0);

	int T;
	cin >> T;
	rep(i, T) {
		cin >> n >> A[0] >> A[1] >> A[2] >> D;
		mp.clear();
		cout <<dfs(n)<<endl;
	}
	return 0;
}