qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from math import floor,ceil
import sys
sys.setrecursionlimit(10**7)
def solve():
    N,A,B,C,D=map(int,input().split())
    memo={}
    def rec(x):
        if x in memo:
            return memo[x]
        if x==1:
            return D
        if x==0:
            return 0
        res=D*x
        for y,cost in zip([2,3,5],[A,B,C]):
            res=min(res,rec(x//y)+abs(x-(x//y*y))*D+cost)
            res=min(res,rec((x+y-1)//y)+abs(x-((x+y-1)//y))*D+cost)
            
        memo[x]=res
        return res
    print(rec(N))

T=int(input())
for i in range(T):
    solve()

Fixed code:
import sys
sys.setrecursionlimit(10**7)
def solve():
    N,A,B,C,D=map(int,input().split())
    memo={}
    def rec(x):
        if x in memo:
            return memo[x]
        if x==1:
            return D
        if x==0:
            return 0
        res=D*x
        for y,cost in zip([2,3,5],[A,B,C]):
            res=min(res,rec(x//y)+abs(x-(x//y*y))*D+cost)
            res=min(res,rec((x+y-1)//y)+abs(x-(((x+y-1)//y)*y))*D+cost)
            
        memo[x]=res
        return res
    print(rec(N))

T=int(input())
for i in range(T):
    solve()
Buggy code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
@lru_cache(maxsize=None)
def dfs(x):
    if x == 0:
        return 0
    if x == 1:
        return d
    l2 = x//2
    r2 = (x+1)//2
    l3 = x//3
    r3 = (x+2)//3
    l5 = x//5
    r5 = (x+4)//5
    count = x*d
    count = min(count,abs(l2*2-x)*d+a+dfs(l2))
    count = min(count,abs(r2*2-x)*d+a+dfs(r2))
    count = min(count,abs(l3*3-x)*d+b+dfs(l3))
    count = min(count,abs(r3*3-x)*d+b+dfs(r3))
    count = min(count,abs(l5*5-x)*d+c+dfs(l5))
    count = min(count,abs(r5*5-x)*d+c+dfs(r5))
    return count

t = int(input())
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    print(dfs(n))

Fixed code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)

def solve(x,a,b,c,d):
    @lru_cache(None)
    def dfs(x):
        if x == 0:
            return 0
        if x == 1:
            return d
        l2 = x//2
        r2 = (x+1)//2
        l3 = x//3
        r3 = (x+2)//3
        l5 = x//5
        r5 = (x+4)//5
        count = x*d
        count = min(count,abs(l2*2-x)*d+a+dfs(l2))
        count = min(count,abs(r2*2-x)*d+a+dfs(r2))
        count = min(count,abs(l3*3-x)*d+b+dfs(l3))
        count = min(count,abs(r3*3-x)*d+b+dfs(r3))
        count = min(count,abs(l5*5-x)*d+c+dfs(l5))
        count = min(count,abs(r5*5-x)*d+c+dfs(r5))
        return count
    return dfs(x)
t = int(input())
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    print(solve(n,a,b,c,d))
Buggy code:
from functools import lru_cache

T = int(input())

for t in range(T):
    N, A, B, C, D = map(int, input().split())

    def dist(n):
        if n == 0:
            return 0
        if n == 1:
            return D

        ret = min(
            D * n,
            D * abs(n - n // 5 * 5) + C + dist(n // 5),
            D * abs(n - (n + 4) // 5 * 5) + C + dist((n + 4) // 5),
            D * abs(n - n // 3 * 3) + B + dist(n // 3),
            D * abs(n - (n + 2) // 3 * 3) + B + dist((n + 2) // 3),
            D * abs(n - n // 2 * 2) + A + dist(n // 2),
            D * abs(n - (n + 1) // 2 * 2) + A + dist((n + 1) // 2),
        )

        return ret

    print(dist(N))

Fixed code:
from functools import lru_cache

T = int(input())

for t in range(T):
    N, A, B, C, D = map(int, input().split())

    @lru_cache(maxsize=None)
    def dist(n):
        if n == 0:
            return 0
        if n == 1:
            return D

        ret = min(
            D * n,
            D * abs(n - n // 5 * 5) + C + dist(n // 5),
            D * abs(n - (n + 4) // 5 * 5) + C + dist((n + 4) // 5),
            D * abs(n - n // 3 * 3) + B + dist(n // 3),
            D * abs(n - (n + 2) // 3 * 3) + B + dist((n + 2) // 3),
            D * abs(n - n // 2 * 2) + A + dist(n // 2),
            D * abs(n - (n + 1) // 2 * 2) + A + dist((n + 1) // 2),
        )

        return ret

    print(dist(N))
Buggy code:
from functools import lru_cache

T = int(input())

caseList = []

for i in range(T):
    caseList.append(list(map(int, input().split())))


@lru_cache(maxsize=None)
def solve(n, A, B, C, D):
    if n == 0:
        return 0
    if n == 1:
        return D
    if n == 2:
        return min(D + D, A + D)

    ans = n * D

    for num, cost in zip([2, 3, 5], [A, B, C]):
        ans = min(
            f((n + (num - n % num)) // num, A, B, C, D) + cost + D * (num - n % num),
            f((n - n % num) // num, A, B, C, D) + cost + D * (n % num),
            ans,
        )

    return ans


for N, A, B, C, D in caseList:
    print(solve(N, A, B, C, D))

Fixed code:
from functools import lru_cache

T = int(input())

caseList = []

for i in range(T):
    caseList.append(list(map(int, input().split())))


@lru_cache(maxsize=None)
def solve(n, A, B, C, D):
    if n == 0:
        return 0
    if n == 1:
        return D
    if n == 2:
        return min(D + D, A + D)

    ans = n * D

    for num, cost in zip([2, 3, 5], [A, B, C]):
        ans = min(
            solve((n + (num - n % num)) // num, A, B, C, D) + cost + D * (num - n % num),
            solve((n - n % num) // num, A, B, C, D) + cost + D * (n % num),
            ans,
        )

    return ans


for N, A, B, C, D in caseList:
    print(solve(N, A, B, C, D))
Buggy code:
T = int(input())
for i in range(T):
    n, A, B, C, D = map(int, input().split())
    memo = {}
    
    def herasu(N):
        if N == 0:
            return 0
        if N == 1:
            return D
        if N in memo:
            return memo[N]
        m = 100000000000000000000000
        if N % 2 == 0:
            m = min([m, A + herasu(N//2)])
        else:
            m = min([m, A + herasu(N//2) + D])
            m = min([m, A + herasu(N//2 + 1) + D])
        if N % 3 == 0:
            m = min(m, B + herasu(N//3))
        else:
            m = min([m, B + herasu(N//3) + (B%3) * D])
            m = min([m, B + herasu(N//3 + 1) + (3-(B%3)) * D])
        if N % 5 == 0:
            m = min(m, C + herasu(N // 5))
        else:
            m = min([m, C + herasu(N//5) + (B%5) * D])
            m = min([m, C + herasu(N//5 + 1) + (5-(B%5)) * D])
        m = min([m, D * N])
        memo[N] = m
        return m

    print(herasu(n))


Fixed code:
T = int(input())
for i in range(T):
    n, A, B, C, D = map(int, input().split())
    memo = {}

    def herasu(N):
        if N == 0:
            return 0
        if N == 1:
            return D
        if N in memo:
            return memo[N]
        m = 100000000000000000000000
        if N % 2 == 0:
            m = min([m, A + herasu(N//2)])
        else:
            m = min([m, A + herasu(N//2) + D])
            m = min([m, A + herasu(N//2 + 1) + D])
        if N % 3 == 0:
            m = min(m, B + herasu(N//3))
        else:
            m = min([m, B + herasu(N//3) + (N%3) * D])
            m = min([m, B + herasu(N//3 + 1) + (3-(N%3)) * D])
        if N % 5 == 0:
            m = min(m, C + herasu(N // 5))
        else:
            m = min([m, C + herasu(N//5) + (N%5) * D])
            m = min([m, C + herasu(N//5 + 1) + (5-(N%5)) * D])
        m = min([m, D * N])
        memo[N] = m
        return m

    print(herasu(n))
Buggy code:
import sys,queue,math,copy,itertools,bisect,collections
from heapq import *


def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    for _ in range(NI()):
        N,A,B,C,D = LI()

        q = []
        heapify(q)

        memo = {}

        def push(c,n):
            if memo.get(n,INF) > c :
                heappush(q,(c,n))
                memo[n] = c


        push(0,N)

        while q:
            c,n = heappop(q)
            m = [0] * 9
            if n == 1:
                print(c+D)
                break

            if n % 2 == 0:
                push(c+min(A,n//2*D),n//2)
            else:
                m[1] = m[-1] = 1
            if n % 3 == 0:
                push(c+min(B,n//3*2*D),n//3)
                m[n%3] = m[-(n%3)] = 1
            if n % 5 == 0:
                push(c+min(C,n//5*4*D),n//5)
                m[n%5] = m[-(n%5)] = 1
            for i in range(-4,5):
                if m[i] and n+i > 0: push(c+D*abs(i),n+i)

if __name__ == '__main__':
    main()

Fixed code:
import sys,queue,math,copy,itertools,bisect,collections
from heapq import *


def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    for _ in range(NI()):
        N,A,B,C,D = LI()

        q = []
        heapify(q)

        memo = {}

        def push(c,n):
            if memo.get(n,INF) > c :
                heappush(q,(c,n))
                memo[n] = c


        push(0,N)

        while q:
            c,n = heappop(q)
            m = [0] * 9
            if n == 1:
                print(c+D)
                break

            if n % 2 == 0:
                push(c+min(A,n//2*D),n//2)
            else:
                m[1] = m[-1] = 1
            if n % 3 == 0:
                push(c+min(B,n//3*2*D),n//3)
            else:
                m[3-(n%3)] = m[-(n%3)] = 1
            if n % 5 == 0:
                push(c+min(C,n//5*4*D),n//5)
            else:
                m[5-(n%5)] = m[-(n%5)] = 1
            for i in range(-4,5):
                if m[i] and n+i > 0: push(c+D*abs(i),n+i)

if __name__ == '__main__':
    main()
Buggy code:
import math
from functools import lru_cache

T = int(input())


class Resolver:
    def __init__(self, a, b, c, d):
        self.a = a
        self.b = b
        self.c = c
        self.d = d

    @lru_cache(maxsize=None)
    def resolve(self, n):
        if n == 0:
            return 0
        elif n == 1:
            return self.d
        else:
            div5f = n // 5
            div5c = (n + 4) / 5
            div3f = n // 3
            div3c = (n + 2) / 3
            div2f = n // 2
            div2c = (n + 1) / 2
            return min(
                self.d * n,
                self.d * abs(n - div5f * 5) + self.c + self.resolve(div5f),
                self.d * abs(n - div5c * 5) + self.c + self.resolve(div5c),
                self.d * abs(n - div3f * 3) + self.b + self.resolve(div3f),
                self.d * abs(n - div3c * 3) + self.b + self.resolve(div3c),
                self.d * abs(n - div2f * 2) + self.a + self.resolve(div2f),
                self.d * abs(n - div2c * 2) + self.a + self.resolve(div2c),
            )


for _ in range(T):
    N, A, B, C, D = map(int, input().split(' '))
    resolver = Resolver(A, B, C, D)
    print(resolver.resolve(N))


Fixed code:
import math
from functools import lru_cache

T = int(input())


class Resolver:
    def __init__(self, a, b, c, d):
        self.a = a
        self.b = b
        self.c = c
        self.d = d

    @lru_cache(maxsize=None)
    def resolve(self, n):
        if n == 0:
            return 0
        elif n == 1:
            return self.d
        else:
            div5f = n // 5
            div5c = (n + 4) // 5
            div3f = n // 3
            div3c = (n + 2) // 3
            div2f = n // 2
            div2c = (n + 1) // 2
            return min(
                self.d * n,
                self.d * abs(n - div5f * 5) + self.c + self.resolve(div5f),
                self.d * abs(n - div5c * 5) + self.c + self.resolve(div5c),
                self.d * abs(n - div3f * 3) + self.b + self.resolve(div3f),
                self.d * abs(n - div3c * 3) + self.b + self.resolve(div3c),
                self.d * abs(n - div2f * 2) + self.a + self.resolve(div2f),
                self.d * abs(n - div2c * 2) + self.a + self.resolve(div2c),
            )


for _ in range(T):
    N, A, B, C, D = map(int, input().split(' '))
    resolver = Resolver(A, B, C, D)
    print(resolver.resolve(N))
Buggy code:
import sys
sys.setrecursionlimit(10**9)
from functools import lru_cache
from math import ceil as C

t = int(input())
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    @lru_cache(None)
    def f(k):
      if k == 1:return d
      if k == 0:return 0
      return min(k*d,f(k//2)+a+k%2*d,f(C(k//2))+a+-k%2*d,f(k//3)+b+k%3*d,f(C(k//3))+b+-k%3*d,f(k//5)+c+k%5*d,f(C(k//5))+c+-k%5*d)
    print(f(n))

Fixed code:
import sys
sys.setrecursionlimit(10**9)
from functools import lru_cache

t = int(input())
  
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    @lru_cache(None)
    def f(k):
      if k == 1:return d
      if k == 0:return 0
      return min(k*d,f(k//5)+c+k%5*d,f(k//3)+b+k%3*d,f(k//2)+a+k%2*d,f((k+4)//5)+c+-k%5*d,f((k+2)//3)+b+-k%3*d,f((k+1)//2)+a+-k%2*d)
    print(f(n))
Buggy code:
from functools import lru_cache
@lru_cache(None)
def f(k):
    if k==1:return d
    if k==0:return 0
    return min(k*d,f(k//2)+a+k%2*d,f((k+1)//2)+a+-k%2*d,f(k//3)+b+k%3*d,f((k+2)//3)+b+-k%3*d,f(k//5)+c+k%5*d,f((k+4)//5)+c+-k%5*d)
for _ in range(int(input())):
    n,a,b,c,d=map(int,input().split())
    print(f(n))

Fixed code:
from functools import lru_cache
for _ in range(int(input())):
    n,a,b,c,d=map(int,input().split())
    @lru_cache(None)
    def f(k):
        if k==0:return 0
        if k==1:return d
        return min(k*d,f(k//5)+c+k%5*d,f(k//3)+b+k%3*d,f(k//2)+a+k%2*d,f((k+4)//5)+c+-k%5*d,f((k+2)//3)+b+-k%3*d,f((k+1)//2)+a+-k%2*d)
    print(f(n))
Buggy code:
def f(k):
    if k==1:return d
    if k==0:return 0
    if k in dic:return m[k]
    s=k*d
    s=min(s,f(k//2)+a+k%2*d,f((k+1)//2)+a+-k%2*d,f(k//3)+b+k%3*d,f((k+2)//3)+b+-k%3*d,f(k//5)+c+k%5*d,f((k+4)//5)+c+-k%5*d)
    m[k]=s
    return s
for _ in range(int(input())):
    m={}
    n,a,b,c,d=map(int,input().split())
    print(s(n))

Fixed code:
def f(k):
    if k==1:return d
    if k==0:return 0
    if k in m:return m[k]
    s=k*d
    s=min(s,f(k//2)+a+k%2*d,f((k+1)//2)+a+-k%2*d,f(k//3)+b+k%3*d,f((k+2)//3)+b+-k%3*d,f(k//5)+c+k%5*d,f((k+4)//5)+c+-k%5*d)
    m[k]=s
    return s
for _ in range(int(input())):
    m={}
    n,a,b,c,d=map(int,input().split())
    print(f(n))
Buggy code:
T = int(input())

for t in range(T):
    N, A, B, C, D = map(int, input().split())
    memo = {}

    def dist(n):
        if n == 0:
            return 0
        if n in memo:
            return memo[n]
        
        ret = min(
            D * n,
            D * abs(n - n//5*5) + C + dist(n//5),
            D * abs(n - (n+4)//5*5) + C + dist((n+4)//5),
            D * abs(n - n//3*3) + B + dist(n//3),
            D * abs(n - (n+2)//3*3) + B + dist((n+2)//3),
            D * abs(n - n//2*2) + A + dist(n//2),
            D * abs(n - (n+1)//2*2) + A + dist((n+1)//2)
        )

        memo[n] = ret
        return ret
    print(dist(N))

Fixed code:
T = int(input())

for t in range(T):
    N, A, B, C, D = map(int, input().split())
    memo = {}

    def dist(n):
        if n == 0:
            return 0
        if n == 1:
            return D
        if n in memo:
            return memo[n]
        
        ret = min(
            D * n,
            D * abs(n - n//5*5) + C + dist(n//5),
            D * abs(n - (n+4)//5*5) + C + dist((n+4)//5),
            D * abs(n - n//3*3) + B + dist(n//3),
            D * abs(n - (n+2)//3*3) + B + dist((n+2)//3),
            D * abs(n - n//2*2) + A + dist(n//2),
            D * abs(n - (n+1)//2*2) + A + dist((n+1)//2)
        )

        memo[n] = ret
        return ret
    print(dist(N))
Buggy code:
import sys
sys.setrecursionlimit(10**8)
import functools
T=int(input())
@functools.lru_cache(maxsize=None)
def solve(N):
    if N==0:
        return 0
    if N==1:
        return D
    tmp=N*D    
    if N%2==0:
        tmp=min(tmp,solve(N//2)+A)
    else:
        tmp=min(tmp,solve((N-1)//2)+A+D,solve((N+1)//2)+A+D)
    if N%3==0:
        tmp=min(tmp,solve(N//3)+B)
    else:
        b=N%3 
        tmp=min(tmp,solve((N+(3-b))//3)+B+(3-b)*D,solve((N-b)//3)+B+b*D)
    if N%5==0:
        tmp=min(tmp,solve(N//5)+C)
    else:
        c=N%5
        tmp=min(tmp,solve((N-c)//5)+C+D*c,solve((N+(5-c))//5)+C+D*(5-c))
    return tmp

for _ in range(T):
    N,A,B,C,D=map(int,input().split())
    print(solve(N))


Fixed code:
T=int(input())
from functools import lru_cache

for _ in range(T):
    N,A,B,C,D=map(int,input().split())
    @lru_cache(maxsize=None)  
    def solve(N):
        if N==0:
            return 0
        if N==1:
            return D
        tmp=N*D    
        if N%2==0:
            tmp=min(tmp,solve(N//2)+A)
        else:
            tmp=min(tmp,solve((N-1)//2)+A+D,solve((N+1)//2)+A+D)
        if N%3==0:
            tmp=min(tmp,solve(N//3)+B)
        else:
            b=N%3 
            tmp=min(tmp,solve((N+(3-b))//3)+B+(3-b)*D,solve((N-b)//3)+B+b*D)
        if N%5==0:
            tmp=min(tmp,solve(N//5)+C)
        else:
            c=N%5
            tmp=min(tmp,solve((N-c)//5)+C+D*c,solve((N+(5-c))//5)+C+D*(5-c))
        return tmp

    print(solve(N))
Buggy code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)


memo = {}

def solve():
    N,A,B,C,D = map(int,input().split())
    return process(N,N,A,B,C,D)


def process(M,N,A,B,C,D):
    if M == 1:
        return D
    elif M == 0:
        return 0
    elif M < 0:
        return D * N
    global memo
    if M in memo:
        return memo[M]
    res = process((M-M%2)//2,N,A,B,C,D) + A + (M%2) * D
    res = min(res,process((M+M%2)//2,N,A,B,C,D) + A + (M%2) * D)
    res = min(res,process((M-M%3)//3,N,A,B,C,D) + B + (M%3) * D)
    res = min(res,process((M+3-M%3)//3,N,A,B,C,D) + B + (3-M%3) * D)
    res = min(res,process((M-M%5)//5,N,A,B,C,D) + C + (M%5) * D)
    res = min(res,process((M+5-M%5)//5,N,A,B,C,D) + C + (5-M%5) * D)
    memo[M] = res
    return res


def main():
    T = int(input())

    for i in range(T):
        global memo
        memo = {}
        res = solve()
        print(res)



main()


Fixed code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)


memo = {}

def solve():
    N,A,B,C,D = map(int,input().split())
    global memo
    memo = {0:0,1:D}
    return process(N,N,A,B,C,D)


def process(M,N,A,B,C,D):
    # if M < 0:
    #     return D * N + 1
    global memo
    if M in memo:
        return memo[M]
    res = process((M-M%2)//2,N,A,B,C,D) + A + (M%2) * D
    res = min(res,process((M+M%2)//2,N,A,B,C,D) + A + (M%2) * D)
    res = min(res,process((M-M%3)//3,N,A,B,C,D) + B + (M%3) * D)
    res = min(res,process((M+3-M%3)//3,N,A,B,C,D) + B + (3-M%3) * D)
    res = min(res,process((M-M%5)//5,N,A,B,C,D) + C + (M%5) * D)
    res = min(res,process((M+5-M%5)//5,N,A,B,C,D) + C + (5-M%5) * D)
    res = min(res,M*D)
    memo[M] = res
    return res


def main():
    T = int(input())

    for i in range(T):
        res = solve()
        print(res)



main()
Buggy code:
T = int(input())

testcase = [list(map(int,input().split())) for i in range(T)]

from math import ceil
from functools import lru_cache

@lru_cache(maxsize=None)
def f(n,a,b,c,d):
    if n == 0:
        return 0
    if n == 1:
        return d
    else:
        return min((n-5*(n//5)) * d + c + f(n//5,a,b,c,d),(n-3*(n//3)) * d + b + f(n//3,a,b,c,d),(n-2*(n//2)) * d + a + f(n//2,a,b,c,d),
                   (5*ceil(n/5)-n) * d + c + f(ceil(n/5),a,b,c,d),(3*ceil(n/3)-n) * d + b + f(ceil(n/3),a,b,c,d),(2*ceil(n/2)-n) * d + a + f(ceil(n/2),a,b,c,d),n*d)

for n,a,b,c,d in testcase:
    print(f(n,a,b,c,d))

Fixed code:
T = int(input())

testcase = [list(map(int,input().split())) for i in range(T)]

from functools import lru_cache

@lru_cache(maxsize=None)
def f(n,a,b,c,d):
    if n == 0:
        return 0
    if n == 1:
        return d
    else:
        return min((n-5*(n//5)) * d + c + f(n//5,a,b,c,d),(n-3*(n//3)) * d + b + f(n//3,a,b,c,d),(n-2*(n//2)) * d + a + f(n//2,a,b,c,d),
                   (5*((n+4)//5)-n) * d + c + f((n+4)//5,a,b,c,d),(3*((n+2)//3)-n) * d + b + f((n+2)//3,a,b,c,d),(2*((n+1)//2)-n) * d + a + f((n+1)//2,a,b,c,d),n*d)

for n,a,b,c,d in testcase:
    print(f(n,a,b,c,d))
Buggy code:
import sys
sys.setrecursionlimit(10**8)
T=int(input())

def solve(N):
    if N==0:
        return 0
    if N==1:
        return D
    if N in dic:
        return dic[N]
    tmp=N*D    
    if N%2==0:
        tmp=min(tmp,solve(N//2)+A)
    else:
        tmp=min(tmp,solve((N-1)//2)+A+D)
    b=N%3 
    if N%3==0:
        tmp=min(tmp,solve(N//3)+B)
    else:
        tmp=min(tmp,solve((N+(3-b))//3)+B+D,solve((N-b)//3)+B+2*D)
    c=N%5
    if N%5==0:
        tmp=min(tmp,solve(N//5)+C)
    else:
        tmp=min(tmp,solve((N-c)//5)+C+D*c,,solve((N+(5-c))//5)+C+D*(5-c))
    dic[N]=tmp
    return tmp

for _ in range(T):
    N,A,B,C,D=map(int,input().split())
    dic={}
    print(solve(N))


Fixed code:
import sys
sys.setrecursionlimit(10**8)
T=int(input())

def solve(N):
    if N==0:
        return 0
    if N==1:
        return D
    if N in dic:
        return dic[N]
    tmp=N*D    
    if N%2==0:
        tmp=min(tmp,solve(N//2)+A)
    else:
        tmp=min(tmp,solve((N-1)//2)+A+D,solve((N+1)//2)+A+D)
    if N%3==0:
        tmp=min(tmp,solve(N//3)+B)
    else:
        b=N%3 
        tmp=min(tmp,solve((N+(3-b))//3)+B+(3-b)*D,solve((N-b)//3)+B+b*D)
    if N%5==0:
        tmp=min(tmp,solve(N//5)+C)
    else:
        c=N%5
        tmp=min(tmp,solve((N-c)//5)+C+D*c,solve((N+(5-c))//5)+C+D*(5-c))
    dic[N]=tmp
    return tmp

for _ in range(T):
    N,A,B,C,D=map(int,input().split())
    dic={}
    print(solve(N))
Buggy code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq


def solve():
  N, A, B, C, D = map(int, input().split())
  seen_values = set()
  queue = []
  heapq.heappush(queue, (0, N))

  while True:
    cost, value = heapq.heappop(queue)

    if value in seen_values:
      continue

    seen_values.add(value)

    if value == 1:
      print(cost + D)
      return

    for k, c in ((2, A), (3, B), (5, C)):
      v, m = divmod(value, k)

      if v == 0:
        continue
      elif m == 0:
        heapq.heappush(queue, (cost + c, v))
      else:
        heapq.heappush(queue, (cost + c + D * m, v))
        heapq.heappush(queue, (cost + c + D * (k - m), v + 1))


def main():
  for _ in range(int(input())):
    solve()


if __name__ == '__main__':
  main()


Fixed code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq


def solve():
  N, A, B, C, D = map(int, input().split())
  seen_values = set()
  queue = []
  heapq.heappush(queue, (0, N))

  while True:
    cost, value = heapq.heappop(queue)

    if value in seen_values:
      continue

    seen_values.add(value)

    if value == 1:
      print(cost + D)
      return

    for k, c in ((2, A), (3, B), (5, C)):
      v, m = divmod(value, k)

      if v == 0:
        continue
      elif m == 0:
        heapq.heappush(queue, (cost + c, v))
      else:
        heapq.heappush(queue, (cost + c + D * m, v))
        heapq.heappush(queue, (cost + c + D * (k - m), v + 1))

      heapq.heappush(queue, (cost + D * (value - v), v))


def main():
  for _ in range(int(input())):
    solve()


if __name__ == '__main__':
  main()