from math import ceil,floor,log
from decimal import *
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
for i in cases:
    #qappend=q.append
    l2=[0]*3
    N,l2[0],l2[1],l2[2],D=i
    num=0
    d = {}
    #print(l2)
    for j in range(ceil(log(N,2))):         #2の累乗
        for k in range(ceil(log(N,3))):     #3の累乗
            for l in range(ceil(log(N,5))): #5の累乗
                div=pow(2,j)*pow(3,k)*pow(5,l)
                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))
    heapify(q)
    while len(q)>0:
        dist,u=heappop(q)
        if d[u]<dist: continue
        for j in range(3):
            num=1+pow(2,j)
            const=l2[j]
            x = Decimal(u) / Decimal(num)
            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(const+((num-(u%num))%num)*D,(u-v1)*D)
            alt2 = d[u] + min(const+(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))

### 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()