#include <stdio.h>

int min(float a[]);

int main (void){
    int t;
    scanf("%d",&t);
    int n,a[4],ans=0;
    int b[3] = {2,3,5};
    float cost[4];
    int x=0;
    int minnum=0;

   // for(int i = 0;i <t;i++){
        scanf("%d %d %d %d %d",&n,&a[0],&a[1],&a[2],&a[3]);
        ans += a[3];//最初の一枚
        x ++;

        while(1){
            if(n ==x)break;
            if(n < x){
                ans += a[3]*(x-n);
                break;
            }
            cost[0] = (float)a[0]/(float)(b[0]*x-1);
            cost[1] = (float)a[1]/(float)(b[1]*x-1);
            cost[2] = (float)a[2]/(float)(b[2]*x-1);
            cost[3] = (float)a[3];
            minnum = min(cost);
            printf("%d\n",minnum);
            if(minnum==3){
                x ++;
                ans += a[3];
            }else{
                x *= b[minnum];
                ans += a[minnum];
            }
        }
        printf("%d\n",ans);
   // }

}

int min(float a[]){
    int min = 0;
    float m = a[0];
    printf("%f\n",a[0]);
    for(int i = 1;i<4;i++){
        printf("%f\n",a[i]);
        if(a[i] < m){
                 min = i;
                 m = a[i];
        }
    }
    return min;
}