### BUGGY CODE ###

from math import ceil,floor
from decimal import *
import heapq
T=int(input())
cases=[list(map(int,input().split())) for _ in range(T)]
getcontext().prec=20
for i in cases:
    N,A,B,C,D=i
    d = {}
    for i in range(60):         #2の累乗
        for j in range(38):     #3の累乗
            for k in range(26): #5の累乗
                div=pow(2,i)*pow(3,j)*pow(5,k)
                x=Decimal(N)/Decimal(div)
                c=x.to_integral_exact(rounding=ROUND_CEILING)
                f=x.to_integral_exact(rounding=ROUND_FLOOR)
                if d.get(c)==None:
                    d[c]=float("inf")
                if d.get(f)==None:
                    d[f]=float("inf")
    d[N]=0
    #print(d)
    q=[]
    q.append([0,N])
    heapq.heapify(q)
    while len(q)>0:
        dist,u=heapq.heappop(q)
        if d[u]<dist: continue
        for j in range(3):
            if j==0:    #2で割る時を考える
                x=Decimal(u)/Decimal(2)
                v1=x.to_integral_exact(rounding=ROUND_CEILING)
                v2=x.to_integral_exact(rounding=ROUND_FLOOR)
                #v1=ceil(u/2)
                #v2=floor(u/2)
                alt1=d[u]+min(A+(u%2)*D,(u-v1)*D)
                alt2=d[u]+min(A+(u%2)*D,(u-v2)*D)
                if d[v1]>alt1:
                    d[v1]=alt1
                    heapq.heappush(q,[alt1,v1])
                if d[v2]>alt2:
                    d[v2]=alt2
                    heapq.heappush(q,[alt2,v2])
            if j==1:    #3で割る時を考える
                x=Decimal(u)/Decimal(3)
                v1=x.to_integral_exact(rounding=ROUND_CEILING)
                v2=x.to_integral_exact(rounding=ROUND_FLOOR)
                #v1=ceil(u/3)
                #v2=floor(u/3)
                alt1=d[u]+min(B+((3-(u%3))%3)*D,(u-v1)*D)
                alt2=d[u]+min(B+(u%3)*D,(u-v2)*D)
                if d[v1]>alt1:
                    d[v1]=alt1
                    heapq.heappush(q,[alt1,v1])
                if d[v2]>alt2:
                    d[v2]=alt2
                    heapq.heappush(q,[alt2,v2])
            if j==2:    #5で割る時を考える
                x=Decimal(u)/Decimal(5)
                v1=x.to_integral_exact(rounding=ROUND_CEILING)
                v2=x.to_integral_exact(rounding=ROUND_FLOOR)
                #v1=ceil(u/5)
                #v2=floor(u/5)
                alt1=d[u]+min(C+((5-(u%5))%5)*D,(u-v1)*D)
                alt2=d[u]+min(C+(u%5)*D,(u-v2)*D)
                if d[v1]>alt1:
                    d[v1]=alt1
                    heapq.heappush(q,[alt1,v1])
                if d[v2]>alt2:
                    d[v2]=alt2
                    heapq.heappush(q,[alt2,v2])
        #print(d)
        #input()
    print(d[0])
    #print(len(d))

### FIXED CODE ###

from math import ceil,floor,log
#from decimal import Decimal,ROUND_CEILING,ROUND_FLOOR,getcontext
from heapq import *
import sys
input=sys.stdin.readline
T=int(input())
cases=[list(map(int,input().split())) for _ in range(T)]
#getcontext().prec=20
num=0
cnt=0
def main():
    for i in cases:
        l2=[0]*3
        N,l2[0],l2[1],l2[2],D=i
        d = {}
        num=0
        cnt=0
        for j in range(60):         #2の累乗
            for k in range(38):     #3の累乗
                for l in range(26): #5の累乗
                    div=pow(2,j)*pow(3,k)*pow(5,l)
                    x=N//div
                    #x=Decimal(N)/Decimal(div)
                    if N%div!=0:
                        c=x+1
                    else:
                        c=x
                    f=x
                    if d.get(c)==None:
                        d[c]=float("inf")
                    if d.get(f)==None:
                        d[f]=float("inf")
        d[N]=0
        #print(d)
        q=[]
        q.append((0,N))
        heapify(q)
        #print(d)
        while True:
            dist,u=heappop(q)
            if d[u]<dist: continue
            if u==0:
                print(dist)
                break
            for j in range(3):
                num=1+pow(2,j)
                cnt=l2[j]
                x =u//num
                if u%num!=0:
                    v1 = x+1
                else:
                    v1 = x
                v2 = x
                # v1=ceil(u/2)
                # v2=floor(u/2)
                alt1 = d[u] + min(cnt+((num-(u%num))%num)*D,(u-v1)*D)
                alt2 = d[u] + min(cnt+(u%num)*D,(u-v2)*D)
                if d[v1] > alt1:
                    d[v1] = alt1
                    heappush(q, (alt1, v1))
                if d[v2] > alt2:
                    d[v2] = alt2
                    heappush(q, (alt2, v2))
            #print(d)
            #input()
        #print(d[0])
        #print(len(d))
if __name__ == '__main__':
    main()