Problem:


錬金マスター

  あなたはついに魔法の釜、錬金釜を手に入れました。錬金釜に複数のアイテムを入れると、新しいア イテムを作ることができます。新しく作ったアイテムは、他のアイテムを作るために錬金釜へ入れることもできます。アイテムを作るために必要なアイテムのリストを記したものを錬金レシピと呼ぶことにします。以下の 3 つは錬金レシピの例です。

     
 木片と糸でラケットができる。

     
 お米と水でおにぎりができる。

     
 ラケットとマイクとおにぎりでギターができる。

  アイテムはお金を使って手にいれることもできますが、錬金釜で作った方が安くすむ場合もあります。例えば、各アイテムの購入価格が以下のように与えられているとします。

アイテム名 購入価格(円)

   木片            3,000

   糸                800

   お米                36

   水                  0

   ラケット           5,000

   マイク            9,800

   おにぎり             140

   ギター           98,000

 ラケットは 5,000 円で購入できますが、木片と糸を購入して錬金すれば 3,800 円で手に入れられます。更に、錬金を重ねることによりとてもお得にアイテムを手に入れられます。図 1 は上記 3 つの錬金レシピ例を組み合わせたものです。ギターは 98,000 円で購入できますが、木片、糸、お米、水、マイクを購入して錬金すれば、たったの 13,636 円で手に入れられます。

図 1

 あなたは冒険を楽に進めるため、なるべく安く目的のアイテムを手に入れようと考えました。 アイテムのリスト、錬金レシピのリスト、指定されたアイテムを入力とし、指定されたアイテムを作るために必要な金額の最小値を出力するプログラムを作成して下さい。

n は 1 以上 100 以下の整数とし、各アイテムを購入する場合の値段 p は 0 以上 1000000 以下とします。アイテムの名前 i は 1 文字以上 100 文字以下のアルファベットからなる半角文字列です。錬金レシピの種類 m は 0 以上 100 以下の整数です。 1つの錬金レシピはアイテムの名前 o、それを作るために必要なアイテムの個数 k 、 k 個のアイテム名 q のリストによって与えられます。k は 100 以下とします。

-->

 各アイテムの数量に限りはないものとします。アイテムは複数のレシピに使われることがありますが、1 つのアイテムを作るためのレシピはたかだか 1 つです。また、あるアイテムを起点としてレシピをたどっていったとき、そのアイテムが再びレシピに現れることはありません。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。

n
s1 p1
s2 p2
:
sn pn
m
o1 k1 q1 q2 ... qk1
o2 k2 q1 q2 ... qk2
:
om km q1 q2 ... qkm
t

1 行目にリストに含まれるアイテムの種類数 n (1 ≤ n ≤ 100) が与えられます。続く n 行に i 番目のアイテムの名前 si (1 文字以上 100 文字以下のアルファベットからなる半角文字列) とそれを購入する場合の値段 pi (0 ≤ pi ≤ 1000000)が与えられます。

続く行に錬金レシピの数 m (0 ≤ m ≤ 100) が与えられます。続く m 行に i 番目のレシピの情報が与えられます。各レシピとして、アイテムの名前 oi、それを作るために必要なアイテムの個数 ki (1 ≤ ki ≤ 100) 、 ki 個のアイテム名 q1, q2 ... qki が与えられます。

最後の行に指定されたアイテム名 t が与えられます。

データセットの数は 30 を超えません。

Output

入力データセットごとに、最小の値段を１行に出力します。

Sample Input

8
wood 3000
string 800
rice 36
water 0
racket 5000
microphone 9800
onigiri 140
guitar 98000
3
racket 2 wood string
onigiri 2 rice water
guitar 3 racket microphone onigiri
guitar
1
computer 300000
0
computer
0

Output for the Sample Input

13636
300000



Sample input:
8
wood 3000
string 800
rice 36
water 0
racket 5000
microphone 9800
onigiri 140
guitar 98000
3
racket 2 wood string
onigiri 2 rice water
guitar 3 racket microphone onigiri
guitar
1
computer 300000
0
computer
0


Sample output:
13636
300000

Submission status: Runtime Error

Solution code:
def price(target, item, recipe):
    ret = []
    
    if target in recipe:
        ret.append(sum(price(r,item, recipe) for r in recipe[target]))
    if target in item:
        ret.append(item[target])

    return min(ret)

import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    item = [f.readline().split() for _ in range(n)]
    item = {name:int(price) for name, price in item}
    recipe = [f.readline().strip().split() for _ in range(int(f.readline()))]
    recipe = {r[0]:r[2:2 + int(r[1])] for r in recipe}
    print(price(f.readline().strip(), item, recipe))

Submission status: Runtime Error

Solution code:
def check(item):
    price = 0
    for i in recipe[item[-1]]:
        if recipe.has_key(i) == False:
            price += pricelist[i]
        else:
            item.append(i)
            price += min(check(item), pricelist[item[-1]])
            item.pop()
    return price

while True:
    pricelist = {}
    recipe = {}

    n = input()
    if n == 0: break

    for i in range(n):
        item, price = raw_input().split()
        pricelist[item] = int(price)

    m = input()
    for i in range(m):
        data = raw_input().split()
        recipe[data[0]] = []
        for j in range(int(data[1])):
            recipe[data[0]].append(data[j + 2])

    price = check([raw_input()])
    print price

Submission status: Runtime Error

Solution code:
while True:
  count =int(input())
  if count == 0:break
  thing1,money1=[],[]
  for i in range(count):
      hoge = map(str,raw_input().split())
      thing1.append(hoge[0])
      a = int(hoge[1])
      money1.append(a)
  count2 = int(input())
  if count2 == 0: 
      a = map(str,raw_input())
      a = ''.join(a)
      b = thing1.index(a)
      print money1[b]
    for i in range(count2):
      hage=map(str,raw_input().split())
      hage[1] = int(hage[1])
      x,h=2,0
      for j in range(hage[1]):
        t = thing1.index(hage[x])
        h += money1[t]
        x+=1
      t = thing1.index(hage[0])
      g = money1[t]
      if h < g: money1[t] = h
  a = map(str,raw_input())
  a =''.join(a)
  ans =thing1.index(a)
  print money1[ans]

Submission status: Runtime Error

Solution code:
while True:
  count =int(input())
  if count == 0:break
  thing1,money1=[],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    thing1.append(hoge[0])
    a = int(hoge[1])
    money1.append(a)
  count2 = int(input())
  if count2 == 0: 
      a = map(str,raw_input())
      a = ''.join(a)
      b = thing1.index(a)
      print money1[b]
  for i in range(count2):
      hage=map(str,raw_input().split())
      hage[1] = int(hage[1])
      x,h=2,0
      for j in range(hage[1]):
        t = thing1.index(hage[x])
        h += money1[t]
        x+=1
      t = thing1.index(hage[0])
      g = money1[t]
      if h < g: money1[t] = h
  a = map(str,raw_input())
  a =''.join(a)
  ans =thing1.index(a)
  print money1[ans]

Submission status: Runtime Error

Solution code:
while True:
  count = int(input())
  if count == 0: break
  thing1,thing2,money=[],[],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    q = int(hoge[1])
    thing1.append(hoge[0])
    money.append(q)
  count = int(input())
  if count == 0 :
    hoge = raw_input()
    ans=thing1.index(hoge)
    print money[ans]
  else:
    for i in range(count):
      hoge = map(str,raw_input().split())
      q = int(hoge[1])
      kn=0
      for j in range(q):
        e = thing1.index(hoge[j+2])
        kn += money[e]
      e =thing1.index(hoge[0])
      money[e] = kn
    ans = raw_input()
    hagehage =thing1.index(ans)
    ans = int(money[hagehage])
    print ans

Submission status: Runtime Error

Solution code:
while True:
  count = int(input())
  if count == 0: break
  thing1,thing2,money=[],[],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    q = int(hoge[1])
    thing1.append(hoge[0])
    money.append(q)
  count = int(input())
  if count == 0 :
    hoge = raw_input()
    ans=thing1.index(hoge)
    print money[ans]
  else:
    for i in range(count):
      hoge = map(str,raw_input().split())
      q = int(hoge[1])
      kn=0
      vv=2
      for j in range(q):
        e = thing1.index(hoge[vv])
        kn += money[e]
        vv+=1
      e =thing1.index(hoge[0])
      money[e] = kn
    ans = raw_input()
    hagehage =thing1.index(ans)
    ans = int(money[hagehage])
    print ans

Submission status: Runtime Error

Solution code:
while True:
 count = int(input())
 if count == 0: break
 thing,money = [],[]
 for i in range(count):
  hoge= map(str,raw_input().split())
  q = int(hoge[1])
  thing.append(hoge[0])
  money.append(q)
 count2 = int(input())
 if count2 == 0:
   hoge= raw_input()
   kz = thing.index(hoge)
   print money[kz]
 else:
   for j in range(count2):
   	hoge = map(str,raw_input().split())
   	q = int(hoge[1])
   	x,y = 2,0
   	for i in range(q):
    	  w = thing.index(hoge[x])
    	  y +=money[w]
    	  x+=1
   	w = thing.index(hoge[0])
   	money[w] = y
   ans = raw_input()
   w = thing.index(ans)
   print money[w]

Submission status: Runtime Error

Solution code:
while True:
  count = int(input())
  if count == 0: break
  money,thing = [],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    t = int(hoge[1])
    money.append(t)
    thing.append(hoge[0])
  count = int(input())
  if count == 0:
    hoge = raw_input()
    t = thing.index(hoge)
    print money[t]
  else:
   for j in range(count):
    hoge = map(str,raw_input().split())
    if hoge[0] in thing:
      t = int(hoge[1])
      x,kane=2,0
      for i in range(t):
        kz = thing.index(hoge[x])
        kane += money[kz]
        x+=1
      t = thing.index(hoge[0])
      money[t] = kane
   hoge = raw_input()
   ans = thing.index(hoge)
   print money[ans]

Submission status: Runtime Error

Solution code:
while True:
  count = int(input())
  if count == 0: break 
  thing,money=[],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    t = int(hoge[1])
    thing.append(hoge[0])
    money.append(t)
  count = int(input())
  if count == 0:
    hoge = raw_input()
    if hoge not in thing:
      print 0
      continue
    t = thing.index(hoge)
    print money[t]
  else:
    for i in range(count):
      hoge = map(str,raw_input().split())
      if hoge[0] not in thing:
        print 0
      t = int(hoge[1])
      x,y = 2,0
      for j in range(t):
        r = thing.index(hoge[x])
        y += money[r]
        x+=1
      e = thing.index(hoge[0])
      money[e]= y
    g = raw_input()
    ans = thing.index(g)
    print money[ans]

Submission status: Runtime Error

Solution code:
while True:
  count = int(input())
  if count == 0: break 
  thing,money=[],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    t = int(hoge[1])
    thing.append(hoge[0])
    money.append(t)
  count = int(input())
  if count == 0:
    hoge = raw_input()
    if hoge not in thing:
      print 0
      continue
    t = thing.index(hoge)
    print money[t]
  else:
    for i in range(count):
      hoge = map(str,raw_input().split())
      if hoge[0] not in thing:
        print 0
        continue
      t = int(hoge[1])
      x,y = 2,0
      for j in range(t):
        r = thing.index(hoge[x])
        y += money[r]
        x+=1
      e = thing.index(hoge[0])
      money[e]= y
    g = raw_input()
    ans = thing.index(g)
    print money[ans]

Submission status: Runtime Error

Solution code:
while True:
    things = {}
    count = input()
    if count == 0: break
    for i in range(count):
        fuga = raw_input().split()
        things[fuga[0]] = int(fuga[1])
    for i in range(input()):
        fuga = raw_input().split()
        money =0
        for j in range(int(fuga[1])):
            money+= things[fuga[j+2]]
        things[fuga[0]] = things[fuga[0]] if money > things[fuga[0]] else money
    ans = raw_input()
    fin = things[ans] if ans in things else 0
    print fin

Submission status: Runtime Error

Solution code:
while True:
&#160;&#160;&#160;&#160;ts={}
&#160;&#160;&#160;&#160;c=input()
&#160;&#160;&#160;&#160;if c==0: break
&#160;&#160;&#160;&#160;for i in range(c):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;f=raw_input().split()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ts[f[0]]=int(f[1])
&#160;&#160;&#160;&#160;for i in range(input()):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;f=raw_input().split()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;m=0
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for j in range(int(f[1])):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;h=ts[f[j+2]] if f[j+2] in ts else 0
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;m+=h
	q=ts[f[0]]
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ts[f[0]]=q if m > q else m
&#160;&#160;&#160;&#160;print ts[raw_input()]

Submission status: Runtime Error

Solution code:
#!/usr/bin/python


import sys




class Alchemey(object):
    def __init__(self):
        self.prices = {}
        self.recipes = {}

    def add_to_price_list(self, line):
        name, price = line.split()
        self.prices[name] = int(price)

    def add_to_recipe_list(self, line):
        xs = line.split()
        dst = xs[0]
        n = int(xs[1])
        srcs = xs[2:]
        self.recipes[dst] = srcs

    def plan(self, name):
        if name not in self.recipes:
            return self.prices[name]
        return sum([self.plan(n) for n in self.recipes[name]])


al = Alchemey()


'PRICE'
'RECIPE'
'REQUEST'

state = None
offset = 0
price_to_read = 0
recipe_to_read = 0
for i, line in enumerate(sys.stdin):
    i = i - offset
    if state is None:
        price_to_read = int(line)
        state = 'PRICE'
        continue
    elif state == 'PRICE' and price_to_read == 0:
        recipe_to_read = int(line)
        state = 'RECIPE'
        continue
    elif state == 'RECIPE' and recipe_to_read == 0:
        state = 'REQUEST'
        pass
    if price_to_read > 0:
        assert state == 'PRICE'
        al.add_to_price_list(line)
        price_to_read -= 1
    elif recipe_to_read > 0:
        assert state == 'RECIPE'
        al.add_to_recipe_list(line)
        recipe_to_read -= 1
    else:
        assert state == 'REQUEST'
        assert recipe_to_read == 0 and price_to_read == 0
        request = line.split()[0]
        offset +=i
        print al.plan(request)
        state = None

Submission status: Runtime Error

Solution code:
#!/usr/bin/python

import sys

class Alchemey(object):
    def __init__(self):
        self.prices = {}
        self.recipes = {}

    def add_to_price_list(self, line):
        name, price = line.split()
        self.prices[name] = int(price)

    def add_to_recipe_list(self, line):
        xs = line.split()
        dst = xs[0]
        n = int(xs[1])
        srcs = xs[2:]
        self.recipes[dst] = srcs

    def plan(self, name):
        if name not in self.recipes:
            return self.prices[name]
        return sum([self.plan(n) for n in self.recipes[name]])


al = Alchemey()


'PRICE'
'RECIPE'
'REQUEST'

state = None
offset = 0
price_to_read = 0
recipe_to_read = 0
for i, line in enumerate(sys.stdin):
    i = i - offset
    if state is None:
        price_to_read = int(line)
        state = 'PRICE'
        continue
    elif state == 'PRICE' and price_to_read == 0:
        recipe_to_read = int(line)
        state = 'RECIPE'
        continue
    elif state == 'RECIPE' and recipe_to_read == 0:
        state = 'REQUEST'
        pass
    if price_to_read > 0:
        assert state == 'PRICE'
        al.add_to_price_list(line)
        price_to_read -= 1
    elif recipe_to_read > 0:
        assert state == 'RECIPE'
        al.add_to_recipe_list(line)
        recipe_to_read -= 1
    else:
        assert state == 'REQUEST'
        assert recipe_to_read == 0 and price_to_read == 0
        request = line.split()[0]
        offset +=i
        print al.plan(request)
        state = None

Submission status: Runtime Error

Solution code:
q,w,e=range,input,raw_input
while True:
  n=w()
  if n==0:break
  d={}
  for i in q(n):
    o=e().split()
    d[o[0]]=int(o[1])
  for j in q(w()):
    t=e().split()
    a=sum([d[i] for i in t[2:]])
    d[t[0]]=a if a<d[t[0]] else d[t[0]]
  print d[e()]

Submission status: Runtime Error

Solution code:
while True:
    n = input()
    if n == 0:
        break
    dic = {}
    for _ in xrange(n):
        name, cost = raw_input().split()
        dic[name] = int(cost)
    for _ in xrange(input()):
        items = raw_input().split()
        dic[items[0]] = min(dic[items[0]], sum(map(lambda x:dic[x], items[2:])))
    print dic[raw_input()]

Submission status: Runtime Error

Solution code:
while True:
    n = input()
    if n == 0:
        break
    dic = {}
    for _ in xrange(n):
        name, cost = raw_input().split()
        dic[name] = int(cost)
    for _ in xrange(input()):
        items = raw_input().split()
        if items[1] == 0:
            continue
        dic[items[0]] = min(dic[items[0]], sum(map(lambda x:dic[x], items[2:])))
    print dic[raw_input()]

Submission status: Runtime Error

Solution code:
while True:
    n = input()
    if n == 0:
        break
    dic = {}
    for _ in xrange(n):
        name, cost = raw_input().split()
        dic[name] = int(cost)
    for _ in xrange(input()):
        items = raw_input().split()
        if items[1] == 0:
            continue
        if items[0] not in dic:
            dic[items[0]] = sum(map(lambda x:dic[x], items[2:]))
        else:
            dic[items[0]] = min(dic[items[0]], sum(map(lambda x:dic[x], items[2:])))
    print dic[raw_input()]

Submission status: Runtime Error

Solution code:
while True:
    n = input()
    if n == 0:
        break
    dic = {}
    for _ in xrange(n):
        name, cost = raw_input().split()
        dic[name] = int(cost)
    m = input()
    recipe = []
    for _ in xrange(m):
        data = raw_input().split()
        recipe.append([data[0]]+data[2:])
    for _ in xrange(m):
        for i in xrange(m):
            dic[recipe[i][0]] = min(recipe[i][0], 
                                    sum(map(lambda x:dic[x], recipe[i][1:])))
    print dic[raw_input()]

Submission status: Runtime Error

Solution code:
while True:
    n = input()
    if n == 0:
        break
    dic = {}
    for _ in xrange(n):
        name, cost = raw_input().split()
        dic[name] = int(cost)
    m = input()
    recipe = []
    for _ in xrange(m):
        data = raw_input().split()
        if data[1] <= 0:
            m -= 1
            continue
        recipe.append([data[0]]+data[2:])
    for _ in xrange(m):
        for i in xrange(m):
            dic[recipe[i][0]] = min(recipe[i][0], 
                                    sum(map(lambda x:dic[x], recipe[i][1:])))
    print dic[raw_input()]

Submission status: Runtime Error

Solution code:
def rec(name):
    if name not in combs:
        return items[name]
    s = 0
    for nm in combs[name]:
        s += rec(nm)
    return min(s, items[name])

while True:
    items = {}
    combs = {}
    n = input()
    if n == 0:
        break
    for i in range(n):
        name, cost = raw_input().split()
        items[name] = int(cost)
    m = input()
    for i in range(m):
        tmp = raw_input().split()
        combs[tmp[0]] = tmp[2:]
    itemname = raw_input()
    print rec(itemname)

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
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()

Submission status: Runtime Error

Solution code:
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()

Submission status: Runtime Error

Solution code:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		n,c = map(str,raw_input().split())
		item[n] = int(c)
	m = input()
	recipes = [map(str,raw_input().split()) for i in range(m)]
	recipes = sorted(recipes, key = lambda x:item[x[0]])
	for recipe in recipes:
		needs = recipe[2:]
		cost = 0
		for need in needs:
			cost += item[need]
		item[recipe[0]] = min(cost, item[recipe[0]])
	print item[raw_input()]

Submission status: Runtime Error

Solution code:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	m = input()
	recipes = [raw_input().split() for i in range(m)]
	recipes = sorted(recipes, key = lambda x:item[x[0]])
	for recipe in recipes:
		name = recipe[0]
		needs = recipe[2:]
		cost = 0
		for need in needs:
			cost += item[need]
		item[name] = min(cost, item[name])
	print item[raw_input()]

Submission status: Runtime Error

Solution code:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	m = input()
	recipes = [raw_input().split() for i in range(m)]
	recipes = sorted(recipes, key = lambda x: item[x[0]] if x[0] in item else 2<<31)
	for recipe in recipes:
		name = recipe[0]
		needs = recipe[2:]
		cost = 0
		for need in needs:
			cost += item[need]
		item[name] = min(cost, item[name]) if name in item else cost
	print item[raw_input()]

Submission status: Runtime Error

Solution code:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	m = input()
	recipes = [raw_input().split() for i in range(m)]
	for recipe in recipes:
		if recipe[0] not in item:
			item[recipe[0]] = 2<<31
	recipes = sorted(recipes, key = lambda x: item[x[0]])
	for recipe in recipes:
		name = recipe[0]
		needs = recipe[2:]
		cost = 0
		for need in needs:
			cost += item[need]
		item[name] = min(cost, item[name])
	print item[raw_input()]

Submission status: Runtime Error

Solution code:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	m = input()
	recipes = [raw_input().split() for i in range(m)]
	for recipe in recipes:
		name = recipe[0]
		cost = 0
		for need in recipe[2:]:
			cost += item[need]
		item[name] = min(cost, item[name])
	print item[raw_input()]