#include<stdio.h>
#include<stdlib.h>

long long int min(long long int,long long int y,long long int z,long long int w);

int main(void){
	long long int n,a,b,c,d;
	int t;
	scanf("%d",&t);
	long long int *ans;
	ans=(long long int*)malloc(10*sizeof(long long int));
	
	for(int j=0;j<t;j++){
		long long int i,m;
		scanf("%lld %lld %lld %lld %lld",&n,&a,&b,&c,&d);
		m=(2*a)/(3) > (3*b)/(4) ? (2*a)/(3) : (3*b)/(4);
		m= m > (5*c)/(6) ? m : (5*c)/(6);
		m=m/d+3+2*n;
		//long long int *cost;
		//cost=(long long int*)malloc(m*sizeof(long long int));
		long long int cost[m];
		cost[n]=0;
		
		for(i=n+1;i<m;i++){
			cost[i]=cost[i-1]+d;
			//printf("%lld %lld\n",i,cost[i]);
		}
		
		for(i=n-1;i>0;i--){
			if(5*i>=m&&3*i>=m&&2*i>=m){
				cost[i]=cost[i+1]+d;
			}else if(5*i>=m&&3*i>=m){
				cost[i]=(cost[2*i]+a)<(cost[i+1]+d) ? cost[2*i]+a : cost[i+1]+d;
				//printf("%lld %lld",cost[i+1],(cost[i+1]+c));
			}else if(5*i>=m){
				cost[i]=(cost[2*i]+a)<(cost[i+1]+d) ? cost[2*i]+a : cost[i+1]+d;
				if(cost[3*i]+b<cost[i]){
					cost[i]=cost[3*i]+b;
				}
			}else{
				cost[i]=min(cost[2*i]+a,cost[3*i]+b,cost[5*i]+c,cost[i+1]+d);
			}
			
			printf("%lld %lld\n",i,cost[i]);
		
		}
		ans[j]=cost[1]+d;
		free(cost);
	}
	for(int j=0;j<t;j++){
		printf("%lld\n",ans[j]);
	}
}

long long int min(long long int x,long long int y,long long int z,long long int w){
	long long int temp1,temp2,ret;
	temp1=x<y?x:y;
	temp2=z<w?z:w;
	ret=temp1<temp2?temp1:temp2;
	return ret;
}