Buggy python submission (verdict: Runtime Error):
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()]

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	for i in range(input()):
		recipe = raw_input().split()
		cost = sum(item[name] if name in item else 0 for name in recipe[2:])
		item[recipe[0]] = min(cost, item[recipe[0]])
	print item[raw_input()]

Buggy python submission (verdict: Runtime Error):
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()]

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	for i in range(input()):
		recipe = raw_input().split()
		cost = sum(item[name] if name in item else 0 for name in recipe[2:])
		item[recipe[0]] = min(cost, item[recipe[0]])
	print item[raw_input()]

Buggy python submission (verdict: Runtime Error):
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()]

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	for i in range(input()):
		recipe = raw_input().split()
		cost = sum(item[name] if name in item else 0 for name in recipe[2:])
		item[recipe[0]] = min(cost, item[recipe[0]])
	print item[raw_input()]

Buggy python submission (verdict: Runtime Error):
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()]

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	for i in range(input()):
		recipe = raw_input().split()
		cost = sum(item[name] if name in item else 0 for name in recipe[2:])
		item[recipe[0]] = min(cost, item[recipe[0]])
	print item[raw_input()]

Buggy python submission (verdict: Runtime Error):
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()]

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	item = {}
	for i in range(n):
		name,cost = raw_input().split()
		item[name] = int(cost)
	for i in range(input()):
		recipe = raw_input().split()
		cost = sum(item[name] if name in item else 0 for name in recipe[2:])
		item[recipe[0]] = min(cost, item[recipe[0]])
	print item[raw_input()]