Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
		"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		having := map[string]Entry{}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			name := buf[0]
			price, _ := strconv.Atoi(buf[1])
			having[name] = Entry{price, []string{}}
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			name_, price_ := buf_[0], INF
			if _, ok := having[name_]; ok {
				price_ = having[name_].price
			}
			having[name_] = Entry{price_, buf_[2:]}
		}
		scanner.Scan()
		fmt.Println(calculation(scanner.Text(), having))
	}
}

type Entry struct {
	price int
	elements []string
}

// メモ再帰的
func calculation(target string, having map[string]Entry) int {
	ans := INF
	if len(having[target].elements) > 0 {
		ans = 0
		for _, item := range having[target].elements {
			ans += calculation(item, having)
		}
	}
	return min(ans, having[target].price)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Python: items = {}
recipes = {}


def reset():
    items.clear()
    recipes.clear()


def algorithm():
    while True:
        reset()

        for _ in range(int(input())):
            item, price = input().split()
            items[item] = int(price)

        if len(items) == 0:
            break

        for _ in range(int(input())):
            recipe = input().split()
            recipes[recipe[0]] = recipe[2:]

        for _ in range(len(recipes)):
            for k, v in recipes.items():
                total = 0
                for i in range(len(v)):
                    total += items[v[i]]
                items[k] = items[k] if items[k] < total else total

        print(items[input()])


def main():
    algorithm()


if __name__ == '__main__':
    main()

Python: 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().split() for _ in range(int(f.readline()))]
    recipe = {r[0]:r[2:] for r in recipe}
    print(price(f.readline().strip(), item, recipe))

Python: 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])
    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().split() for _ in range(int(f.readline()))]
    recipe = {r[0]:r[2:] for r in recipe}
    print(price(f.readline().strip(), item, recipe))

Python: while True:
  item_num = int(input())
  if item_num == 0:
    break

  price_dict = {}
  for _ in range(item_num):
    item, price = input().split()
    price_dict[item] = int(price)

  recipe_num = int(input())
  recipe_dict = {}
  for _ in range(recipe_num):
    recipe = input().split()
    recipe_dict[recipe[0]] = recipe[2:]

  result_dict = {}

  def get_price(item):
    if item in result_dict:
      return result_dict[item]
    if item not in recipe_dict:
      result_dict[item] = price_dict[item]
      return price_dict[item]

    price = 0
    for recipe_item in recipe_dict[item]:
      price += get_price(recipe_item)
    result = min(price, price_dict[item])
    result_dict[item] = result
    return result

  item = input()
  print(get_price(item))

Python: # -*- coding: utf-8 -*-
import sys
#import pdb;pdb.set_trace()
    
if False:
    fh = open('vol2_0201.txt', 'r')
else:
    fh = sys.stdin
    
def input():
    return(fh.readline())

def tot(item, pr, rec):
    if item in rec:
        ret = min(pr[item], sum([tot(p,pr,rec) for p in rec[item]]))
    else:
        ret = pr[item]

    return(ret)
        
while True:
    pr={}
    rec={}
    n = int(input())
    if n==0:
        break
    for _ in range(n):
        v = input().strip().split()
        pr[v[0]] = int(v[1])
    m = int(input())
    for _ in range(m):
        v = input().strip().split()
        rec[v[0]] = v[2:]

    item=input().strip()
    #sys.exit()
    print(tot(item, pr, rec))

Python: def get_price(name, prices, recipe):
    return min(prices[name],
               sum(get_price(_name, prices, recipe) for _name in recipe[name])
               if name in recipe else float("inf")
               )


while True:
    n = int(input())
    if not n:
        break
    prices, recipe = {}, {}
    for _ in [0]*n:
        s, p = input().split()
        prices[s] = int(p)
    for _ in [0]*int(input()):
        a = input().split()
        recipe[a[0]] = a[2:]

    print(get_price(input(), prices, recipe))

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

Python: 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])
    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))

Python: 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))

Python: while True:
  n = int(input())
  if n == 0:
    break

  def_dic = {}
  for _ in range(n):
    s, p = input().split()
    def_dic[s] = int(p)
  
  m = int(input())
  com_dic = {}
  for _ in range(m):
    lst = input().split()
    com_dic[lst[0]] = lst[2:]
  
  price_dic = {}
  
  def get_price(t):
    if t in price_dic:
      return price_dic[t]
    if t not in com_dic:
      price_dic[t] = def_dic[t]
      return def_dic[t]
    
    x = 0
    for q in com_dic[t]:
      x += get_price(q)
    ret = min(x, def_dic[t])
    price_dic[t] = ret
    return ret
  
  print(get_price(input()))