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<algorithm>
#include<cassert>
#include<map>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define N 100

const int INF = 1000100; 
int table[N];
int buycost[N];

int solve(int now,vector<int> *make){
  if ( table[now] != -1)return table[now];
  if(  make[now].size()==0)return table[now]=buycost[now];
  
  int ret = buycost[now],tmp=0;  
  rep(i,make[now].size()){
    tmp+=solve(make[now][i],make);
  }
  
  ret= min(ret,tmp);
  return table[now]=tmp; 
}

main(){
  int n;
  while(cin>>n && n){
    map<string,int> M;
    rep(i,n){
      string t;int a;
      cin>>t>>a;
      M[t]=i;
      buycost[i]=a;
      table[i]=-1;
    }
    
    vector<int> make[n];
    cin>>n;
    rep(i,n){
      string a;
      cin>>a;
      int now =M[a],m;
      cin>>m;
      rep(j,m){
	cin>>a;
	make[now].push_back(M[a]);
      }     
    }
    string tmp;
    cin>>tmp;
    int tar=M[tmp];
    cout << solve(tar,make) << endl;
    
  }
  return false;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

map<string, int> value;
map<string, vector<string> > recipe;

int recur(string item)
{
	if (!recipe[item].size())
		return value[item];

	int s = 0;
	for (int i = 0; i < recipe[item].size(); ++i)
		s += recur(recipe[item][i]);
	return min(value[item], s);
}
int main()
{
	int n;
	while (cin >> n, n)
	{
		value.clear();
		recipe.clear();
		for (int i = 0; i < n; ++i)
		{
			string a;
			int v;
			cin >> a >> v;
			value[a] = v;
		}
		int m;
		cin >> m;
		for (int i = 0; i < m; ++i)
		{
			string a;
			int k;
			cin >> a >> k;
			for (int j = 0; j < k; ++j)
			{
				string b;
				cin >> b;
				recipe[a].push_back(b);
			}
		}

		string item;
		cin >> item;
		cout << recur(item) << endl;
	}

	return 0;
}

C++: 
#include <iostream>

using namespace std;

struct item
{
	int id;
	int price;
};

struct recipe
{
	int id;
	int size;
	int price;
	int ingre[100];
};

item I[100];
recipe R[100];
string T[100];

int solve(int id, int n, int m);

int main(int argc, char** argv)
{
	
	string name, o, q;
	int n, m, p, k;
	
	while( 1 )
	{
		cin >> n;
		if(n==0) break;
		for(int i=0; i<n; i++)
		{
			cin >> T[i];
			I[i].id = i;
			cin >> I[i].price;
		}
		
		cin >> m;
		for(int i=0; i<m; i++)
		{
			cin >> name;
			for(int j=0; j<n; j++)
			{
				if(T[j]==name)
				{
					R[i].id = j;
				}
			}
			cin >> R[i].size;
			
			R[i].price = 0;
			for(int j=0; j<R[i].size; j++)
			{
				cin >> name;
				for(int k=0; k<n; k++)
				{
					if(T[k]==name)
					{
						R[i].ingre[j] = k;
						R[i].price += I[k].price;
					}
				}
			}
		}
		
		cin >> name;
		for(int i=0; i<n; i++)
		{
			if(name==T[i])
			{
				cout << solve(i, n, m) << endl;
			}
		}
	}
	
	return 0;
}

int solve(int id, int n, int m)
{
	int money = 0;
	int min = 100000000;
	bool is_proc = false;
	
	for(int j=0; j<m; j++)
	{
		if(R[j].id == id)
		{
			is_proc = true;
			money = 0;
			for(int k=0; k<R[j].size; k++)
			{
				money += solve(R[j].ingre[k], n, m);
			}
			if(money < min) min = money;
			break;
		}
	}
	
	if(is_proc) return min;
	else return I[id].price;
}

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

using namespace std;

map<string,int> s1;
map<string,vector<string> > s2;
int INF=100000000;
int gold(string s){
	int res=0;
	int res2=INF;
	res=s1[s];
	if(s2[s].size()>0)res2=0;
	for(int i=0;i<s2[s].size();i++){
		res2+=gold(s2[s][i]);
	}
	res=min(res,res2);
	return res;
}

int main(void){
	int n,m;
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		s1.clear();
		s2.clear();
		for(int i=0;i<n;i++){
			string str;
			int c;
			cin >> str >> c;
			s1[str]=c;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			string str;
			int k;
			cin >> str >> k;
			for(int j=0;j<k;j++){
				string s;
				cin >> s;
				s2[str].push_back(s);
			}
		}
		string s3;
		cin >> s3;
		printf("%d\n",gold(s3));
	}
    return 0;
}

C++: #include<iostream>
#include<map>
#include<cstdio>
#include<string>
using namespace std;
int main(){
	int n,m,en,cnt,a,b,c;
	string str,str2;
	while(1){
		map<string,int> data;
		cin >> n;
		if(n==0) break;
		for(int i=0;i<n;i++){
			cin >> str >> en;
			data[str]=en;
		}
		cin >> m;
		for(int i=0;i<m;i++){
			cnt = 0;
			cin >> str >> a;
			for(int j=0;j<a;j++){
				cin >> str2;
				cnt += data[str2];
			}
			if(data[str] > cnt){
				data[str] = cnt;
			}
		}
		cin >> str;
		cout << data[str] << endl;	
	}
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
long long int a,s[101],d,f[101],g=10000001,h[101],q,w,e;
char z[101][101],x[101][101],c[101][101][101],v[101];
int main(void)
{
	int i,j,k;
	while(1){
		scanf("%lld",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			scanf("%s %lld",z[i],&s[i]);
		}
		for(i=0;i<a-1;i++){
			for(j=i+1;j<a;j++){
				if(strcmp(z[i],z[j])==0){
					if(s[i]<s[j]) s[j]=s[i];
					else s[i]=s[j];
				}
			}
		}
		scanf("%lld",&d);
		for(i=0;i<d;i++){
			scanf("%s %lld",x[i],&f[i]);
			for(j=0;j<f[i];j++){
				scanf("%s",c[i][j]);
			}
			h[i]=0;
		}
		scanf("%s",v);
		for(i=0;i<a;i++) if(strcmp(v,z[i])==0) if(g>s[i]) g=s[i];
		for(i=0;i<d;i++){
			for(j=0;j<d;j++){
				for(k=0;k<f[j];k++){
					if(strcmp(x[i],c[j][k])==0) h[j]++;
				}
			}
		}
		if(d!=0){
			w=0;
			while(1){
				for(i=0;i<d;i++){
					q=0;
					e=0;
					if(h[i]==0){
						w++;
						for(j=0;j<a;j++){
							for(k=0;k<f[i];k++){
								if(strcmp(z[j],c[i][k])==0){
									q+=s[j];
									break;
								}
							}
						}
						for(j=0;j<a;j++){
							if(strcmp(z[j],x[i])==0){
								if(s[j]>q){
									s[j]=q;
									q=0;
									break;
								}
							}
						}
					}
					h[i]--;
				}
				if(w==d) break;
			}
		}
		for(i=0;i<a;i++){
			if(strcmp(v,z[i])==0){
				printf("%lld\n",s[i]);
				break;
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
	int n,m;
	while(cin>>n&&n){
		map<string,long long> data;
		string s;
		int a;
		for(int i=0;i<n;i++){
			cin>>s>>a;
			data[s]=a;
		}
		cin>>m;
		string wrought[101][101];
		int k,sum;
		for(int i=0;i<m;i++){
			cin>>wrought[i][0];
			sum=0;
			cin>>k;
			for(int j=0;j<k;j++){
				cin>>wrought[i][j+1];
				sum+=data[wrought[i][j+1]];
			}
			if(data[wrought[i][0]]>sum)data[wrought[i][0]]=sum;
		}
		for(int i=0;i<m;i++){
			int sum=0;
			for(int j=0;j<k;j++)sum+=data[wrought[i][j+1]];
			if(data[wrought[i][0]]>sum)data[wrought[i][0]]=sum;
		}
		string ans;
		cin>>ans;
		cout<<data[ans]<<endl;
	}
}