Title: Alchemy Master

You have finally acquired a magical cauldron, an alchemy cauldron. By placing multiple items into the cauldron, you can create new items. The newly created items can also be placed back into the cauldron to create other items. The list of items required to create an item is referred to as an alchemy recipe. Below are three examples of alchemy recipes:

1. A racket can be made with wood and string.
2. Onigiri can be made with rice and water.
3. A guitar can be made with a racket, a microphone, and onigiri.

Items can be obtained by purchasing them with money, but it may be cheaper to create them in the alchemy cauldron. For example, if the purchase prices for each item are given as follows:

Item Name | Purchase Price (Yen)
- Wood: 3000
- String: 800
- Rice: 36
- Water: 0
- Racket: 5000
- Microphone: 9800
- Onigiri: 140
- Guitar: 98000

A racket can be purchased for 5000 yen, but by purchasing wood and string and using the cauldron, it can be obtained for 3800 yen. Furthermore, by stacking alchemy, items can be obtained very economically. Figure 1 combines the above three alchemy recipe examples. A guitar can be purchased for 98000 yen, but by purchasing wood, string, rice, water, and a microphone and using the cauldron, it can be obtained for just 13636 yen.

You have decided to create a program to output the minimum amount of money required to create a specified item, in order to progress through your adventure more easily. The program will take as input a list of items, a list of alchemy recipes, and the specified item, and output the minimum amount of money required to create the specified item.

Assumptions:
- The number of item types in the item list, n, is an integer between 1 and 100.
- The price, p, for purchasing each item is between 0 and 1000000.
- The name of each item, i, is an alphanumeric string of 1 to 100 characters.
- The number of alchemy recipes, m, is an integer between 0 and 100.
- Each alchemy recipe is given by the item name, o, the number of required items, k, and a list of k item names, q.
- The quantity of each item is unlimited.
- Each item can be used in multiple recipes, but each item has at most one recipe to create it.
- When following a recipe, an item will not reappear in subsequent recipes.

Input:
Multiple datasets are provided as input, ending with a single line containing only zero. Each dataset is in the following format:
- The number of item types, n (1 ≤ n ≤ 100), followed by n lines containing the name, s_i, and purchase price, p_i, for each item.
- The number of alchemy recipes, m (0 ≤ m ≤ 100), followed by m lines containing the recipe information for each recipe, including the item name, o_i, the number of required items, k_i, and a list of k_i item names, q.
- The specified item name, t.

Output:
For each input dataset, output the minimum price on a single line.

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

# Buggy version:
def price(target, item, recipe):
    ret = [0]
    
    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:] for r in recipe}
    print(price(f.readline().strip(), item, recipe))

# Fixed version:
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])
    if len(ret) == 0:
        return 0

    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:] for r in recipe}
    print(price(f.readline().strip(), item, recipe))