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

Fixed submission:
INF = 1 << 29
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(dic[recipe[i][0]] if recipe[i][0] in dic else INF, 
                                    sum(map(lambda x:dic[x] if x in dic else INF, recipe[i][1:])))
    print dic[raw_input()]

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

Fixed submission:
INF = 1 << 29
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(dic[recipe[i][0]] if recipe[i][0] in dic else INF, 
                                    sum(map(lambda x:dic[x] if x in dic else INF, recipe[i][1:])))
    print dic[raw_input()]

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

Fixed submission:
INF = 1 << 29
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(dic[recipe[i][0]] if recipe[i][0] in dic else INF, 
                                    sum(map(lambda x:dic[x] if x in dic else INF, recipe[i][1:])))
    print dic[raw_input()]

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

Fixed submission:
INF = 1 << 29
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(dic[recipe[i][0]] if recipe[i][0] in dic else INF, 
                                    sum(map(lambda x:dic[x] if x in dic else INF, recipe[i][1:])))
    print dic[raw_input()]

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

Fixed submission:
INF = 1 << 29
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(dic[recipe[i][0]] if recipe[i][0] in dic else INF, 
                                    sum(map(lambda x:dic[x] if x in dic else INF, recipe[i][1:])))
    print dic[raw_input()]