from heapq import heappop, heappush



T = int(input())

ans = []

for _ in range(T):

    N, A, B, C, D = map(int, input().split())



    Q = [(0, N)]

    # already processed

    S = set()

    an = D * N

    while Q:

        cost, num = heappop(Q)

        if num in S:

            continue

        an = min(an, cost + num * D)

        if num == 1:

            break

        S.add(num)



        q, r = divmod(num, 2)

        if r < 2:

            heappush(Q, (cost + A + r * D, q))

        if r > 0:

            heappush(Q, (cost + A + (2 - r) * D, q + 1))



        q, r = divmod(num, 3)

        if r < 2:

            heappush(Q, (cost + B + r * D, q))

        if r > 0:

            heappush(Q, (cost + B + (3 - r) * D, q + 1))



        q, r = divmod(num, 5)

        if r < 3:

            heappush(Q, (cost + C + r * D, q))

        if r > 0:

            heappush(Q, (cost + C + (5 - r) * D, q + 1))

    ans.append(an)



print(*ans, sep="\n")

from heapq import heappop, heappush



T = int(input())

ans = []

for _ in range(T):

    N, A, B, C, D = map(int, input().split())



    Q = [(0, N)]

    # already processed

    S = set()

    an = D * N

    while Q:

        cost, num = heappop(Q)

        if num in S:

            continue

        an = min(an, cost + num * D)

        if num == 1:

            break

        S.add(num)



        q, r = divmod(num, 2)

        if r < 2:

            heappush(Q, (cost + A + r * D, q))

        if r > 0:

            heappush(Q, (cost + A + (2 - r) * D, q + 1))



        q, r = divmod(num, 3)

        if r < 2:

            heappush(Q, (cost + B + r * D, q))

        if r > 1:

            heappush(Q, (cost + B + (3 - r) * D, q + 1))



        q, r = divmod(num, 5)

        if r < 3:

            heappush(Q, (cost + C + r * D, q))

        if r > 2:

            heappush(Q, (cost + C + (5 - r) * D, q + 1))

    ans.append(an)



print(*ans, sep="\n")

from heapq import heappop, heappush

T = int(input())
ans = []
for _ in range(T):
    N, A, B, C, D = map(int, input().split())
    Q = [(0, N)]
    # already processed
    S = set()
    an = D * N
    while Q:
        cost, num = heappop(Q)
        if num in S:
            continue
        an = min(an, cost + num * D)
        if num == 1:
            break
        S.add(num)
        q, r = divmod(num, 2)
        if r < 2:
            heappush(Q, (cost + A + r * D, q))
        if r > 0:
            heappush(Q, (cost + A + (2 - r) * D, q + 1))
        q, r = divmod(num, 3)
        if r < 2:
            heappush(Q, (cost + B + r * D, q))
        if r > 0:
            heappush(Q, (cost + B + (3 - r) * D, q + 1))
        q, r = divmod(num, 5)
        if r < 3:
            heappush(Q, (cost + C + r * D, q))
        if r > 0:
            heappush(Q, (cost + C + (5 - r) * D, q + 1))
    ans.append(an)
print(*ans, sep="\n")

from heapq import heappop, heappush

T = int(input())
ans = []
for _ in range(T):
    N, A, B, C, D = map(int, input().split())
    Q = [(0, N)]
    # already processed
    S = set()
    an = D * N
    while Q:
        cost, num = heappop(Q)
        if num in S:
            continue
        an = min(an, cost + num * D)
        if num == 1:
            break
        S.add(num)
        q, r = divmod(num, 2)
        if r < 2:
            heappush(Q, (cost + A + r * D, q))
        if r > 0:
            heappush(Q, (cost + A + (2 - r) * D, q + 1))
        q, r = divmod(num, 3)
        if r < 2:
            heappush(Q, (cost + B + r * D, q))
        if r > 1:
            heappush(Q, (cost + B + (3 - r) * D, q + 1))
        q, r = divmod(num, 5)
        if r < 3:
            heappush(Q, (cost + C + r * D, q))
        if r > 2:
            heappush(Q, (cost + C + (5 - r) * D, q + 1))
    ans.append(an)
print(*ans, sep="\n")

-        if r > 0: +        if r > 1: -        if r > 0: +        if r > 2: