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
}

C++: #include<iostream>
#include<string>

using namespace std;

int main()
{
	for(;;)
	{
		string all_item[101][101];
		int item_price[101],item_num[100]={0};
		int item_flag[101]={0};
		int n,m,nm,i,j,k,l,o,y,d;
		
		cin >> n ;
		if(n==0)
			break;
		
		for(i=0;i<n;i++)
			cin >> all_item[i][0] >> item_price[i];
		
		cin >> m ;
		for(i=0;i<m;i++)
		{
			int s_price[101]={0};
			string item;
			cin >> item >> nm ;
			
			for(j=0;j<n;j++)
				if(all_item[j][0]==item)
					break;
			
			for(k=0;k<nm;k++)
			{
				cin >> item ;
				for(y=0;y<n;y++)
				{
					if(item==all_item[y][0])
						break;
				}
				item_num[y]++;
				all_item[y][item_num[y]]=all_item[j][0];
				
				s_price[j]+=item_price[y];
			}
			if(s_price[j]<item_price[j])
			{
				int a;
				a=item_price[j]-s_price[j];
				item_price[j]=s_price[j];
				
				A:;
				
				for(o=1;o<=item_num[j];o++)
				{
					if(all_item[j][o]!="0")
					{
						for(int r=0;r<n;r++)
						{
							if(all_item[j][o]==all_item[r][0])
							{
//								cout << "mt_item:" << all_item[j][0] << "\nfinish_item:" << all_item[j][o] << endl;
								item_price[r]-=a;
								if(item_num[r]>0)
								{
									item_flag[r]=1;
								}
//								all_item[j][o]="0";
								break;
							}
						}
					}
				}
				for(d=0;d<n;d++)
				{
					if(item_flag[d]==1)
					{
						item_flag[d]=0;
						j=d;
						goto A;
					}
				}
			}
		}
		string last;
		cin >> last ;
		for(i=0;i<n;i++)
		{
			if(all_item[i][0]==last)
			{
				cout << item_price[i] << endl;
				break;
			}
		}
	}
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
struct item
{
	string name;
	int cost,size;
	vector<string> le;
};
item lis[100];
int mysearch(string ss)
{
	for(int i=0;i<n;i++)
	{
		if(lis[i].name==ss)return i;
	}
}
int getcost(int v)
{
		if(lis[v].size==0)return lis[v].cost;
		else
		{
			int ans=0;
			for(int i=0;i<lis[v].size;i++)
			{
				ans+=getcost(mysearch(lis[v].le[i]));
			}
			lis[v].cost=min(lis[v].cost,ans);
			return lis[v].cost;
		}
	
}
int main()
{
	while(1)
	{
		cin >> n;
		if(n==0)break;
		string s;
		int c;
		for(int i=0;i<n;i++)
		{
			cin >> s >> c;
			lis[i].name=s;
			lis[i].cost=c;
			lis[i].size=0;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			int t,k;
			cin >> s;
			cin >> t;
			for(int j=0;j<t;j++)
			{
				string st;
				cin >> st;
				k=mysearch(s);
				lis[k].le.pb(st);
			}
			lis[k].size=t;
		}
		string str;
		cin >> str;
		cout << getcost(mysearch(str))<< endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>

using namespace std;

#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

map<string, int> price;
map<string, vector<string> > resipi;

map<string, int> memo;

int solve(string str)
{
	if(memo.count(str))
		return memo[str];
	
	memo[str] = -1;
	
	if(!resipi.count(str))
		return memo[str] = price[str];
	
	int cost = 0;
	rep(i, resipi[str].size()){
		int a = solve(resipi[str][i]);
		if(a == -1)
			return memo[str] = price[str];
		
		cost += a;
	}
	
	return memo[str] = min(price[str], cost);
}

int main()
{
	int n;
	while(scanf("%d", &n), n){
		price.clear();
		resipi.clear();
		memo.clear();
		
		rep(i, n){
			char name[128];
			int p;
			scanf("%s%d", name, &p);
			price[string(name)] = p;
		}
		
		int m;
		scanf("%d", &m);
		rep(i, m){
			char name[128];
			int k;
			scanf("%s%d", name, &k);
			rep(j, k){
				char factor[128];
				scanf("%s", factor);
				resipi[string(name)].push_back(string(factor));
			}
		}
		
		char name[128];
		scanf("%s", name);
		printf("%d\n", solve(string(name)));
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while (cin>>n,n){
        map<string, int>items;
        for (int i = 0; i < n; i++){
            string str;
            int a;
            cin >> str >> a;
            items[str]= a;
        }
        int renkinn;
        cin >> renkinn;
        for (int i = 0; i < renkinn; i++){
            string item;
            int sum = 0, a;
            cin >> item >> a;
            for (int j = 0; j < a; j++){
                string b;
                cin >> b;
                sum += items[b];
            }
            if (sum < items[item])items[item] = sum;
        }
        string ans;
        cin >> ans;
        cout << items[ans] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#define int long long
using namespace std;

int n,p[100],cost[100];
vector<int> recipe[100];

int dfs(int v){
	int sum = 0;
	if(cost[v] != -1) return cost[v];
	if(recipe[v].size() == 0) return cost[v] = p[v];
	for(int i = 0;i < recipe[v].size();i++){
		sum += dfs(recipe[v][i]);
	}
	return cost[v] = min(p[v],sum);
}

signed main(){
	int m;
	string t;
	while(cin >> n,n){
		for(int i = 0;i < 100;i++) {
			recipe[i].clear();
			cost[i] = -1;
		}
		map<string,int> mp;
		for(int i = 0;i < n;i++){
			string s;
			cin >> s >> p[i];
			mp[s] = i;
		}
		cin >> m;
		for(int i = 0;i < m;i++){
			int k;
			string s,q;
			cin >> s >> k;
			for(int j = 0;j < k;j++){
				cin >> q;
				recipe[mp[s]].push_back(mp[q]);
			}
		}
		cin >> t;
		cout << dfs(mp[t]) << endl;
	}
	return 0;
}