#include <stdio.h>

int main(void)
{
  long long int t;
  long long int n[15],a[15],b[15],c[15],d[15];
  long long int sum[15];
  long long int aflag, bflag, cflag;
  long long int flag,dflag;
  long long int obj;

  scanf("%lld", &t);
  for(long long int i=1;i<=t;i++){
    scanf("%lld %lld %lld %lld %lld",&n[i],&a[i],&b[i],&c[i],&d[i]);
    aflag = 2*b[i]*c[i];
    bflag = 3*a[i]*c[i];
    cflag = 5*a[i]*b[i];
    if(aflag >= bflag){
      if(bflag >= cflag){
	flag = 1;    //abc
      } else {
	if(aflag >= cflag){
	  flag = 2;  //acb
	} else {
	  flag = 5;  //cab
	}
      }
    } else {
      if(aflag >= cflag){
	flag = 3;    //bac
      } else {
	if(bflag >= cflag){
	  flag = 4;  //bca
	} else {
	  flag = 6;  //cba
	}
      }
    }
    dflag = 1;
    if(2*d[i]>a[i]){
      dflag = dflag*2;
    }
    if(3*d[i]>b[i]){
      dflag = dflag*3;
    }
    if(5*d[i]>c[i]){
      dflag = dflag*5;
    }
    for(long long int j=0;j<flag;j++){
      if(n[i]-1-j%flag==0){
	if(flag>=2*j){
	  obj = n[i]-1-j;
	  break;
	} else {
	  obj = n[i]-1-j+flag;
	  break;
	}
      }
    }
    switch(flag){
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    }
  }   //for_end
  for(long long int i=1;i<=t;i++){
    printf("%lld\n", sum[i]);
  }
  return 0;
}
