Buggy python submission (verdict: Runtime Error):
import sys

def cheaperprice(item):
    rawprice=rawprices[item]
    if item not in menus:
        return rawprice
    else:
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)
        return min(combinedprice,rawprice)


n=0 #how many numbers of lists
flag=0
rawprices={}
menus={}

for line in sys.stdin:
    line=line.strip()
    if n==0:
        if flag==0 and line=='0':
            break
        elif flag!=2:
            n=int(line)
        else:
            print cheaperprice(line)
            rawprices={}
            menus={}
        flag=(flag+1)%3
    else:
        line=line.split(' ')
        if flag==1: #rawprices
            rawprices[line[0]]=int(line[1])
        elif flag==2:
            menus[line[0]]=line[2:]
        n-=1

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10000)


def cheaperprice(item):
    rawprice=rawprices[item]
    if item not in menus:
        return rawprice
    else:
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)
        return min(combinedprice,rawprice)


n=0 #how many numbers of lists
flag=0
rawprices={}
menus={}

for line in sys.stdin:
    line=line.strip()
    if n==0:
        if flag==0 and line=='0':
            break
        elif flag!=2:
            n=int(line)
        else:
            print cheaperprice(line)
            rawprices={}
            menus={}
        flag=(flag+1)%3
    else:
        line=line.split(' ')
        if flag==1: #rawprices
            rawprices[line[0]]=int(line[1])
        elif flag==2:
            menus[line[0]]=line[2:]
        n-=1

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(1024*1024)


def cheaperprice(item):
    rawprice=rawprices[item]
    if item not in menus:
        return rawprice
    else:
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)
        return min(combinedprice,rawprice)


n=0 #how many numbers of lists
flag=0
rawprices={}
menus={}

for line in sys.stdin:
    line=line.strip()
    if n==0:
        if flag==0 and line=='0':
            break
        elif flag!=2:
            n=int(line)
        else:
            print cheaperprice(line)
            rawprices={}
            menus={}
        flag=(flag+1)%3
    else:
        line=line.split(' ')
        if flag==1: #rawprices
            rawprices[line[0]]=int(line[1])
        elif flag==2:
            menus[line[0]]=line[2:]
        n-=1

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

def cheaperprice(item):
    rawprice=rawprices[item]
    if item not in menus or item in checkloop:
        checkloop.append(item)
        return rawprice
    else:
        checkloop.append(item)
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)

        return min(combinedprice,rawprice)


n=0 #how many numbers of lists
flag=0
rawprices={}
menus={}
checkloop=[]
for line in sys.stdin:
    line=line.strip()
    if n==0:
        if flag==0 and line=='0':
            break
        elif flag!=2:
            n=int(line)
        else:
            print cheaperprice(line)
            rawprices={}
            menus={}
            checkloop=[]
        flag=(flag+1)%3
    else:
        line=line.split(' ')
        if flag==1: #rawprices
            rawprices[line[0]]=int(line[1])
        elif flag==2:
            menus[line[0]]=line[2:]
        n-=1

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

def cheaperprice(item):
    rawprice=rawprices[item]
    if item in checkloop:
        return rawprice
    elif item not in menus 
        checkloop.append(item)
        return rawprice
    else:
        checkloop.append(item)
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)
        return min(combinedprice,rawprice)


n=0 #how many numbers of lists
flag=0
rawprices={}
menus={}
checkloop=[]
for line in sys.stdin:
    line=line.strip()
    if n==0:
        if flag==0 and line=='0':
            break
        elif flag!=2:
            n=int(line)
        else:
            print cheaperprice(line)
            rawprices={}
            menus={}
            checkloop=[]
        flag=(flag+1)%3
    else:
        line=line.split(' ')
        if flag==1: #rawprices
            rawprices[line[0]]=int(line[1])
        elif flag==2:
            menus[line[0]]=line[2:]
        n-=1

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

def cheaperprice(item):
    rawprice=rawprices[item]
    if item in checkloop:
        return rawprice
    elif item not in menus:
        checkloop.append(item)
        return rawprice
    else:
        checkloop.append(item)
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)
        return min(combinedprice,rawprice)


n=0 #how many numbers of lists
flag=0
rawprices={}
menus={}
checkloop=[]
for line in sys.stdin:
    line=line.strip()
    if n==0:
        if flag==0 and line=='0':
            break
        elif flag!=2:
            n=int(line)
        else:
            print cheaperprice(line)
            rawprices={}
            menus={}
            checkloop=[]
        flag=(flag+1)%3
    else:
        line=line.split(' ')
        if flag==1: #rawprices
            rawprices[line[0]]=int(line[1])
        elif flag==2:
            menus[line[0]]=line[2:]
        n-=1

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

def cheaperprice(item):
    rawprice=rawprices[item]
    if item in checkloop:
        return rawprice
    elif item not in menus:
        checkloop.append(item)
        return rawprice
    else:
        checkloop.append(item)
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i)
        return min(combinedprice,rawprice)

def main():
    n=0 #how many numbers of lists
    flag=0
    rawprices={}
    menus={}
    checkloop=[]
    for line in sys.stdin:
        line=line.strip()
        if n==0:
            if flag==0 and line=='0':
                break
            elif flag!=2:
                n=int(line)
            else:
                print cheaperprice(line)
                rawprices={}
                menus={}
                checkloop=[]
            flag=(flag+1)%3
        else:
            line=line.split(' ')
            if flag==1: #rawprices
                rawprices[line[0]]=int(line[1])
            elif flag==2:
                menus[line[0]]=line[2:]
            n-=1
    return 0
    
main()

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break

Buggy python submission (verdict: Runtime Error):
import sys

def cheaperprice(item,checkloop,rawprices,menus):
    rawprice=rawprices[item]
    if item in checkloop:
        return rawprice
    elif item not in menus:
        checkloop.append(item)
        return rawprice
    else:
        checkloop.append(item)
        indegrients=menus[item]
        combinedprice=0
        for i in indegrients:
            combinedprice+=cheaperprice(i,checkloop,rawprices,menus)
        return min(combinedprice,rawprice)

def main():
    n=0 #how many numbers of lists
    flag=0
    rawprices={}
    menus={}
    checkloop=[]
    for line in sys.stdin:
        line=line.strip()
        if n==0:
            if flag==0 and line=='0':
                break
            elif flag!=2:
                n=int(line)
            else:
                print cheaperprice(line,checkloop,rawprices,menus)
                rawprices={}
                menus={}
                checkloop=[]
            flag=(flag+1)%3
        else:
            line=line.split(' ')
            if flag==1: #rawprices
                rawprices[line[0]]=int(line[1])
            elif flag==2:
                menus[line[0]]=line[2:]
            n-=1
    return 0
    
main()

Fixed submission:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        pricelist=[]
        recipedict={}
        costdict={}
        for i in range(n):
            [ii,p]=raw_input().split()
            pricelist.append([ii,int(p)])
            costdict[ii]=1000000000000
        pricelist.sort(key=lambda x:x[1])
        m=int(raw_input())
        for i in range(m):
            rec=raw_input().split()
            o=rec[0]
            k=int(rec[1])
            recipedict[o]=rec[2:]
        query=raw_input()
        while(1):
            cheapest=pricelist.pop(0)
            #if able to make
            if cheapest[0] in recipedict:
                priceifmade=0
                for reqitem in recipedict[cheapest[0]]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<cheapest[1]:
                    costdict[cheapest[0]]=priceifmade
                else:
                    costdict[cheapest[0]]=cheapest[1]
            else:
                costdict[cheapest[0]]=cheapest[1]
            for item in recipedict:
                priceifmade=0
                for reqitem in recipedict[item]:
                    priceifmade=priceifmade+costdict[reqitem]
                if priceifmade<costdict[item]:
                    costdict[item]=priceifmade
            if cheapest[0]==query:
                print costdict[cheapest[0]]
                break