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

Python: class Data:
    def __init__(self,n,p):
        self.name = str(n)
        self.price = int(p)
        self.recipe = []

    def minCost(self):
        if self.recipe == []:
            return self.price
        else:
            return min(self.price,
                       sum(list(map(lambda x: Data.minCost(index[x]), self.recipe))))
    def str(self):
        return "{}:{}:{}".format(self.name, self.price, self.recipe)

    
i_str = input()
n = int(i_str)
index = {}
while n != 0:
    for i in range(n):
        i_str = input()
        name,price = i_str.split()
        index[name] = Data(name,price)

    i_str = input()
    m = int(i_str)
    for i in range(m):
        i_str = input()
        data = i_str.split()    
        index[data[0]].recipe = data[2:]
            
    q = input()
    print(index[q].minCost())
       
    i_str = input()
    n = int(i_str)
    index = {}

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

Python: items = {}
recipes = {}


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


def algorithm():
    while True:
        item_number = int(input())
        if item_number == 0:
            break
        reset()

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

        recipe_number = int(input())
        for _ in range(recipe_number):
            recipe = input().split()
            r = []
            for i in range(int(recipe[1])):
                r.append(recipe[i + 2])
            recipes[recipe[0]] = r

        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

        target = input()
        print(items[target])


def main():
    algorithm()


if __name__ == '__main__':
    main()

C++: #include <iostream>
#include <vector>
#include <map>
using namespace std;

class Item{
public:
    string name;
    int cost;
    vector<string> subItems;
    int sumCost;
    int calc();
};

const int MAX_N = 100;
Item items[MAX_N + 1];
map<string, int> item_idx;

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        item_idx.clear();
        for (int i = 0; i < n; i++) {
            cin >> items[i].name >> items[i].cost;
            items[i].sumCost = -1;
            item_idx.insert(make_pair(items[i].name, i));
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++) {
            string name;
            int k;
            cin >> name >> k;
            for (int j = 0; j < k; j++) {
                string sub;
                cin >> sub;
                items[item_idx[name]].subItems.push_back(sub);
            }
        }
        string name;
        cin >> name;
        cout << items[item_idx[name]].calc() << endl;
    }
}

int Item::calc() {
    int result = cost;
    if (result > 0 && subItems.size() > 0) {
        int sum = 0;
        if (sumCost < 0) {
            for (vector<string>::iterator it = subItems.begin(); it != subItems.end(); it++) {
                sum += items[item_idx[*it]].calc();
                if (sum > result) {
                    break;
                }
            }
        } else {
            sum = sumCost;
        }
        result = min(result, sum);
    }
    return result;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int main(){
  int n;
  map<string, int> items;
  string name;
  int value;
  
  while(cin >> n, n){
    for(int i = 0 ; i < n ; i++){
      cin >> name >> value;
      items.insert(make_pair(name, value));
    }
    int m;
    int total;
    string names;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
      total = 0;
      cin >> name;
      cin >> m;
      for(int j = 0 ; j < m ; j++){
	cin >> names;
	total += items[names];
      }
      items[name] = min(items[name], total);
    }
    cin >> name;
    cout << items[name] << endl;
  }
  return 0;
}