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: Accepted)

//set many funcs template
//Ver.20190820
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<assert.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384

long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
void llswap(long long *a,long long *b){long long c;c=(*a);(*a)=(*b);(*b)=c;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}

long long search(long long x,long long a[],long long n){
  long long st=0,fi=n-1,te;
  while(st<=fi){
    te=(st+fi)/2;
    if(a[te]<x){st=te+1;}else{fi=te-1;}
  }
  return st;
}

//Dynamic Array

typedef struct{
    long long st;
    long long fi;
    long long kr;
}rs;

typedef struct{
    long long st;
    long long kz;
}mkj;

int sortfnc(const void *a,const void *b){
if(((rs*)a)->st == ((rs*)b)->st){return 0;}
if(((rs*)a)->st < ((rs*)b)->st){return -1;}
return 1;
}

void makemkj(rs g[],mkj x[],long long n){
    long long i,ms=0,nst=g[0].st;
    for(i=1;i<n;i++){
        if(g[i].st!=g[i-1].st){
            x[nst].kz=i-ms;
            x[nst].st=ms;
            nst=g[i].st;ms=i;
        }
    }
    x[nst].kz=n-ms;
    x[nst].st=ms;
}

//Heap and Priority_queue

typedef struct{
long long node;
long long cost;
}data;

data heap[2097152];
long long hsize=0;

void resheap(){
    hsize=0;
    long long i;
    heap[0].node=-1;
    heap[0].cost=-llinf;
    for(i=1;i<2097152;i++){
        heap[i].node=-1;
        heap[i].cost=llinf;
    }
}

int swjud(data high,data low){
    if(high.cost > low.cost){return 1;}
    return -1;
}

void pqpush(data x){
    long long a,b;
    data m;
    hsize++;
    heap[hsize]=x;
    a=hsize;
    while(1){
        b=a/2;
        if(swjud(heap[b],heap[a]) == 1){
            m=heap[a];heap[a]=heap[b];heap[b]=m;
        }
        else{break;}
        a=b;
    }
}

data pqpop(){
    long long a,b,c;
    data m,sw;
    m=heap[1];
    heap[1]=heap[hsize];
    heap[hsize].node=-1;
    heap[hsize].cost=llinf;
    hsize--;
    a=1;
    while(1){
        b=a*2;c=a*2+1;
        if(swjud(heap[a],heap[b])==-1 && swjud(heap[a],heap[c])==-1){break;}
        if(swjud(heap[b],heap[c])==-1){
            sw=heap[a];heap[a]=heap[b];heap[b]=sw;
            a=b;
        }
        else{
            sw=heap[a];heap[a]=heap[c];heap[c]=sw;
            a=c;
        }
    }
    return m;
}

long long dijkstra(long long a,long long b,long long vc,rs g[],mkj x[]){
  //resheap();
  while(hsize>0){pqpop();}
  long long i,j,dist[524288],nv;
  for(i=0;i<=vc;i++){
    dist[i]=llinf;
  }
  data pd,od;
  pd.node=a;
  pd.cost=0;
  pqpush(pd);
  while(hsize>0){
    od=pqpop();
    nv=od.node;
    if(nv==b){return od.cost;}
    if(dist[nv]!=llinf){continue;}
    dist[nv]=od.cost;
    for(i=x[nv].st;i<x[nv].st+x[nv].kz;i++){
      if(dist[g[i].fi]!=llinf){continue;}
      pd.node=g[i].fi;
      pd.cost=od.cost+g[i].kr;
      if(pd.cost>llinf){continue;}
      pqpush(pd);
    }
  }
  return llinf;
}

int main(void){
  resheap();
  long long a,b,c,d;
  long long i,j,n,m,k,h,w,r,l,t;
  long long arr[524288];
  long long ec=0,vc=0;
  rs g[2097152];
  mkj x[1048576];
  scanf("%lld",&t);
  while(t>0){
    r=llinf;
    t--;
    scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d);
    h=1;
    for(i=0;i<=60;i++){
      w=h;
      for(j=0;j<=40;j++){
        l=w;
        for(k=0;k<=20;k++){
          arr[vc]=n/l;vc++;
          arr[vc]=llceil(n,l);vc++;
          l*=5;
          if(l>n){break;}
        }
        w*=3;
        if(w>n){break;}
      }
      h*=2;
      if(h>n){break;}
    }
    qsort(arr,vc,sizeof(long long),llsortfncsj);
    //fprintf(stderr,"%lld\n",vc);
    l=vc;vc=1;
    for(i=1;i<l;i++){
      if(arr[i-1]==arr[i]){continue;}
      arr[vc]=arr[i];
      vc++;
    }
    ec=0;
    for(i=0;i<vc;i++){
      //if(ec>2000000){fprintf(stderr,"edge!!\n");}
      if(i!=(vc-1)){
        if(((double)d)*((double)llzt(arr[i],arr[i+1]))<5.0e18){
          g[ec].st=i;
          g[ec].fi=i+1;
          g[ec].kr=d*llzt(arr[i],arr[i+1]);
          ec++;
          g[ec]=g[ec-1];
          llswap(&g[ec].st,&g[ec].fi);
          ec++;
        }
      }
      for(j=-4;j<=4;j++){
        if(arr[i]+j<=0){continue;}
        
        if((arr[i]+j)%2==0){
          h=search((arr[i]+j)/2,arr,vc);
          if(arr[h]==((arr[i]+j)/2)){
            g[ec].st=i;
            g[ec].fi=h;
            g[ec].kr=d*llzt(0,j)+a;
            ec++;
          }
        }

        if((arr[i]+j)%3==0){
          h=search((arr[i]+j)/3,arr,vc);
          if(arr[h]==((arr[i]+j)/3)){
            g[ec].st=i;
            g[ec].fi=h;
            g[ec].kr=d*llzt(0,j)+b;
            ec++;
          }
        }

        if((arr[i]+j)%5==0){
          h=search((arr[i]+j)/5,arr,vc);
          if(arr[h]==((arr[i]+j)/5)){
            g[ec].st=i;
            g[ec].fi=h;
            g[ec].kr=d*llzt(0,j)+c;
            ec++;
          }
        }
      }
    }
    //fprintf(stderr,"%lld %lld\n",vc,ec);
    qsort(g,ec,sizeof(g[0]),sortfnc);
    makemkj(g,x,ec);
    printf("%lld\n",d+dijkstra(vc-1,0,vc,g,x));
  }
  return 0;
}