#include <stdio.h>

int coins;
int double_a(int a, int num);
int triple_b(int b, int num);
int fifth_c(int c, int num);
int addOrReduce_d(int d, int n, int num);
void func_a(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a1[4][3], int a, int b, int c, int d);
void func_b(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a2[4][3], int a, int b, int c, int d);
void func_c(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a3[4][3], int a, int b, int c, int d);
void func_d(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a4[4][3], int a, int b, int c, int d);
void optimise(int array[4][3], int min[3]);
void answer(int a, int b, int c, int d);

int main(void) {
	int a, b, c, d, n, t, i, j;
	int ans[10] = { 0 };
	int a1[4][3], a2[4][3], a3[4][3], a4[4][3], dp[5];
	int min_a[3], min_b[3], min_c[3], min_d[3];

	scanf("%d", &t);

	for (i = 0; i < t; i++) {
		scanf("%d %d %d %d %d", &n, &a, &b, &c, &d);
		for (j = 0; j < 5; j++) {
			dp[j] = n + j;
		}
		for (j = 0; j < 5; j++) {
			min_a[0] = dp[j] / 2; min_a[1] = dp[j] % 2; min_a[2] = a;
			min_b[0] = dp[j] / 3; min_b[1] = dp[j] % 3; min_b[2] = b;
			min_c[0] = dp[j] / 5; min_c[1] = dp[j] % 5; min_c[2] = c;
			min_d[0] = dp[j] - 1; min_d[1] = 0; min_d[2] = d;
			int needOpt[4] = { 0 };
			while (min_a[0] == 1 && min_b[0] == 1 && min_c[0] == 1 && min_d[0] == 1) {
				if (min_a[0] != 1) {
					func_a(min_a, min_b, min_c, min_d, a1, a, b, c, d);
					needOpt[0] = 1;
				}
				if (min_b[0] != 1) {
					func_a(min_a, min_b, min_c, min_d, a2, a, b, c, d);
					needOpt[1] = 1;
				}
				if (min_c[0] != 1) {
					func_a(min_a, min_b, min_c, min_d, a3, a, b, c, d);
					needOpt[2] = 1;
				}
				if (min_d[0] != 1) {
					func_a(min_a, min_b, min_c, min_d, a4, a, b, c, d);
					needOpt[3] = 1;
				}
				if (needOpt[0] == 1) {
					optimise(a1, min_a);
				}
				if (needOpt[1] == 1) {
					optimise(a2, min_b);
				}
				if (needOpt[2] == 1) {
					optimise(a3, min_c);
				}
				if (needOpt[3] == 1) {
					optimise(a4, min_d);
				}
				for (j = 0; j < 4; j++) {
					needOpt[j] = 0;
				}
			}
			if (min_a[1] != 0) {
				min_a[2] += min_a[1] * d;
			}
			if (min_b[1] != 0) {
				min_b[2] += min_b[1] * d;
			}
			if (min_c[1] != 0) {
				min_c[2] += min_c[1] * d;
			}
			if (min_d[1] != 0) {
				min_d[2] += min_d[1] * d;
			}
			answer(min_a[2], min_b[2], min_c[2], min_d[2]);
		}
		
	}

	for (i = 0; i < t; i++) {
		if (ans[i] != 0) {
			printf("%d\n", ans[i]);
		}
	}

	return 0;
}

void answer(int a, int b, int c, int d) {
	int ans, i;
	int array[4];
	array[0] = a; array[1] = b; array[2] = c; array[3] = d;
	ans = array[0];

	for (i = 1; i < 4; i++) {
		if (array[i] <= ans) {
			ans = array[i];
		}
	}

	printf("%d\n", ans);

}

void optimise(int array[4][3], int min[3]) {
	int temp = 0;
	int i;
	temp = array[0][2];
	for (i = 1; i < 4; i++) {
		if (array[i][2] <= temp) {
			temp = array[i][2];
			min[0] = array[i][0];
			min[1] = array[i][1];
		}
	}
	min[2] = temp;
}

void func_a(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a1[4][3], int a, int b, int c, int d) {
	a1[0][0] = min_a[0] / 2; a1[1][0] = min_b[0] / 2; a1[2][0] = min_c[0] / 2; a1[3][0] = min_d[0] / 2;
	a1[0][1] = min_a[1] + (min_a[0] % 2); a1[1][1] = min_b[1] + (min_b[0] % 2); a1[2][1] = min_c[1] + (min_c[0] % 2); a1[3][1] = min_d[1] + (min_d[0] % 2);
	a1[0][2] = min_a[2] + a; a1[1][2] = min_b[2] + a; a1[2][2] = min_c[2] + a; a1[3][2] = min_d[2] + a;
}

void func_b(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a2[4][3], int a, int b, int c, int d) {
	a2[0][0] = min_a[0] / 3; a2[1][0] = min_b[0] / 3; a2[2][0] = min_c[0] / 3; a2[3][0] = min_d[0] / 3;
	a2[0][1] = min_a[1] + (min_a[0] % 3); a2[1][1] = min_b[1] + (min_b[0] % 3); a2[2][1] = min_c[1] + (min_c[0] % 3); a2[3][1] = min_d[1] + (min_d[0] % 3);
	a2[0][2] = min_a[2] + b; a2[1][2] = min_b[2] + b; a2[2][2] = min_c[2] + b; a2[3][2] = min_d[2] + b;
}

void func_c(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a3[4][3], int a, int b, int c, int d) {
	a3[0][0] = min_a[0] / 5; a3[1][0] = min_b[0] / 5; a3[2][0] = min_c[0] / 5; a3[3][0] = min_d[0] / 5;
	a3[0][1] = min_a[1] + (min_a[0] % 5); a3[1][1] = min_b[1] + (min_b[0] % 5); a3[2][1] = min_c[1] + (min_c[0] % 5); a3[3][1] = min_d[1] + (min_d[0] % 5);
	a3[0][2] = min_a[2] + c; a3[1][2] = min_b[2] + c; a3[2][2] = min_c[2] + c; a3[3][2] = min_d[2] + c;
}

void func_d(int min_a[3], int min_b[3], int min_c[3], int min_d[3], int a4[4][3], int a, int b, int c, int d) {
	a4[0][1] = min_a[1] - 1; a4[1][1] = min_b[1] - 1; a4[2][1] = min_c[1] - 1; a4[3][1] = min_d[1] - 1;
	a4[0][2] = min_a[2] + d; a4[1][2] = min_b[2] + d; a4[2][2] = min_c[2] + d; a4[3][2] = min_d[2] + d;
}

int double_a(int a, int num) {
	num *= 2;
	coins += a;

	return num;
}

int triple_b(int b,int num) {
	num *= 3;
	coins += b;

	return num;
}

int fifth_c(int c, int num){
	num *= 5;
	coins += c;

	return num;
}

int addOrReduce_d(int d, int n, int num) {
	if (num < n) {
		num += 1;
	}
	else if (num > n) {
		num -= 1;
	}
	coins += d;

	return num;
}