#include <stdio.h>
#include <string.h>

int goal,x2,x3,x5,plus1;
int loop(int* db,unsigned long long int now);

int main(void){
    int db[200000]={0};
    int T;
    scanf("%d",&T);
    
    for(int i=0;i<T;i++){
        scanf("%d %d %d %d %d",&goal,&x2,&x3,&x5,&plus1);
        memset(db, 0, sizeof(db));
        db[1]=plus1;
        loop(db,1);
        printf("%d\n",db[goal]);
        for(int i=0;i<goal*2;i++){
            //printf("%d %llu\n",i,db[i]);
        }
    }
    
}

int loop(int* db,unsigned long long int now){
    if(now>goal*2){return 0;}
    //printf("%llu %llu\n",now,db[now]);
    if(now*2<goal*2){
    if(db[now*2]>db[now]+x2 || db[now*2]==0){
        db[now*2]=db[now]+x2;
        //printf("x2 %llu %llu %llu\n",now,now*2,db[now*2]);
        loop(db,now*2);
    }
    }
    
    if(now*3<goal*2){
    if(db[now*3]>db[now]+x3 || db[now*3]==0){
        db[now*3]=db[now]+x3;
        //printf("x3 %llu %llu %llu\n",now,now*3,db[now*3]);
        loop(db,now*3);
    }
    }
    
    if(now*5<goal*2){
    if(db[now*5]>db[now]+x5 || db[now*5]==0){
        db[now*5]=db[now]+x5;
        //printf("x5 %llu %llu %llu\n",now,now*5,db[now*5]);
        loop(db,now*5);
    }
    }
    
    if(now!=1){
    if(db[now-1]>db[now]+plus1|| db[now-1]==0){
    db[now-1]=db[now]+plus1;
    //printf("-1 %llu %llu %llu\n",now,now-1,db[now-1]);
    loop(db,now-1);
    }
    }
    
    if(db[now+1]>db[now]+plus1|| db[now+1]==0){
    db[now+1]=db[now]+plus1;
    //printf("+1 %llu %llu %llu\n",now,now+1,db[now+1]);
    loop(db,now+1);
    }
    
    return 0;
}