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 <stdio.h>
#include <vector>
#include<string>
using namespace std;
int ha(string);
void saga(void);

struct L{
	string name;
	int cost;
};
struct  R{
	string name;
	string zairyo[101];
	int size;
};
int q,m,n;
L list[101];
R resipi[101];

int main()
{	
	int i,j,k,b=0,s,a;
	string S;
	while((cin>> n)!=EOF){
	q=n;
	for(i=0;i<n;i++){
		cin  >> list[i].name>> list[i].cost;
	}


	cin>>m;
	for(i=0;i<m;i++){
		cin>>resipi[i].name>>b;
		resipi[i].size=b;
		for(j=0;j<b;j++){
		cin>>resipi[i].zairyo[j];
		}
		
	}
	cin>>S;

	
	/*for(i=0;i<n;i++){
		cout <<list[i].name<< list[i].cost<<endl;
	}
	for(i=0;i<m;i++){
		cout<<resipi[i].name;
		for(j=0;j<resipi[i].size;j++){
			cout<<resipi[i].zairyo[j];
		}
		cout<<endl;
	}
	*/
	cout<<ha(S)<<endl;
	}
	return 0;

}
int ha(string S)
{
	int  i,j,f=0,sum=0;
	for(i=0;i<m;i++){
		//cout<<S<<endl<<resipi[i].name;	
	if(S==resipi[i].name){
		f=1;
		break;
	}
	}
	if(f==1){
		for(j=0;j<resipi[i].size;j++){
			sum=sum+ha(resipi[i].zairyo[j]);
			}
		//printf("%d\n",sum);
		for(i=0;i<n;i++){
		if(S==list[i].name){
		if(sum<list[i].cost){
		return sum;
		}
		else return list[i].cost;
		}
	}
		
		}

	for(i=0;i<n;i++){
		if(S==list[i].name){
		return list[i].cost;
		}
	
	}

	
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FFOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,b) FOR(i,0,b)
#define RREP(i,b) FFOR(i,1,b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut,pr> ppr;
typedef priority_queue<pr,Vpr, greater<pr> > PQ;
const int SIZE=5+3*1e+4;
const ut INF=1<<30;
const ld eps=1e-6;
const LL p=7+1e+9;

int N, M;
map<string, int> items;
map<string, vector<string>> recipe;

int dfs(string s){
	int res = 0;
	if(recipe[s].empty()){
		return items[s];
	}
	else{
		REP(i,recipe[s].size()){
			res += min(items[recipe[s][i]], dfs(recipe[s][i]));
		}
	}
	return res;
}

int main(){
	while(cin >> N && N){
		string s;
		int n;
		REP(i,N){
			cin >> s >> n;
			items[s] = n;
		}
		cin >> M;
		string c, q;
		int k;
		REP(i,M){
			cin >> c >> k;
			vector<string> tmp;
			REP(j,k){
				cin >> q;
				tmp.push_back(q);
			}
			recipe[c] = tmp;
		}
		string goal;
		cin >> goal;
		cout << dfs(goal) << endl;
	}
}

C++: #include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int N;


class State
{

public:
	string item;
	int cost;

	State(string item, int cost):item(item),cost(cost){}

};

int main(void)
{
	while(cin >> N, N){

		vector<State> items;
		map<string, int> itemmap;
		map<string, vector<string> > itemR;

		string itemstr,itemstr2;
		string queryitem;
		int money;
		for(int i = 0; i < N; i++){
			cin >> itemstr >> money;
			State st(itemstr,money);
			items.push_back(st);
			itemmap[st.item] = st.cost;
		}
		cin >> N;

		int M;
		for(int i = 0; i < N; i++){
			cin >> itemstr;
			cin >> M;

			
			while(M > 0){
				cin >> itemstr2;
				itemR[itemstr].push_back(itemstr2);
				M--;
				
			}
				for(int j = 0; j < items.size(); j++){
					int sum = 1 << 28;
					int mcost = items[j].cost;

				//	printf("\n");
					if(itemR[items[j].item].size()){
						sum = 0;
						for(int k = 0; k < itemR[items[j].item].size();  k++){
							string No = itemR[items[j].item][k];
							sum += itemmap[No];
			//				cout << No << ' ';
						}
						items[j].cost = min(mcost, sum);
						itemmap[items[j].item] = min(mcost, sum);
					//	cout << items[j].item << ' ' << mcost << ' ' << sum << endl;
					
				}

			}
		}
		

		cin >> queryitem;

		int ans = itemmap[queryitem];


		cout << ans << endl;

	//	for(int i = 0; i < items.size(); i++){
	//		cout << items[i].item << items[i].cost << endl;;
	//	}




	}



	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define rep(I,N) for(int I = 0; I < (int)(N); I++)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
map<string, int> money;
map<string, vector<string> > LESP;
int solve(string make){
	vector<string> item = LESP[make];
	ll mini = INF;
	ll cost = 0;
	if(item.size() == 0)return money[make];
	rep(i, item.size()){
		if(!LESP[item[i]].empty()){
			//cout << "item" << endl;
			cost += min(money[item[i]], solve(item[i]));
		}else{
			cost += money[item[i]];
		}
	}
	return cost;
}

int main(void){
	int n, m;
	while(cin >> n, n){
		money.clear();
		LESP.clear();
		string MAKE;
		rep(i, n){
			string s;
			ll cost;
			cin >> s >> cost;
			money[s] = cost;
		}
		cin >> m;
		rep(i, m){
			string s;
			cin >> s;
			int num;
			cin >> num;
			vector<string> v;
			rep(j, num){
				string L;
				cin >> L;
				v.push_back(L);
			}
			LESP[s] = v;
		}
		cin >> MAKE;
		ll ans = min(money[MAKE], solve(MAKE));
		FIN(ans);

	}


	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}
/*
8
wood 3000000
string 800
rice 36
water 10
racket 5000
microphone 9800
onigiri 140
guitar 98000
3
racket 2 wood string
onigiri 2 rice water
guitar 3 racket microphone onigiri
guitar
*/