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<cstdio>
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;


vector<int> list[100];
int cost[100];

int saiki(int n){
	if(list[n].empty()==1){
		return cost[n];
	}
	int cont=0;
	for(int i=0;i<list[n].size();i++){
		cont+=saiki(list[n][i]);
	}
	return min(cont,cost[n]);
}

int main(){
	
	while(1){
		map<string,int> name;
		
		int n;
		scanf("%d",&n);
		
		if(n==0){
			break;
		}
		for(int i=0;i<n;i++){
			string a;int b;
			cin>>a>>b;
			cost[i]=b;
			name[a]=i;
		}
		for(int i=0;i<100;i++){
			list[i].clear();
		}
		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			string a;int b;
			cin>>a>>b;
			int num=name[a];
			for(int j=0;j<b;j++){
				string c;
				cin>>c;
				list[num].push_back(name[c]);
			}
		}
		string a;
		cin>>a;
		
		printf("%d\n",saiki(name[a]));
		
	}
}

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

using namespace std;

class Alchemy{
public:
  string product, material[100];
  int num;
};
main(){
  int n;
  while(1){
    cin >> n;
    if(n==0) break;
    map<string, int> items;
    for(int i=0;i<n;i++){
      string tmp;
      cin >> tmp;
      cin >> items[tmp];
    }

    int m;
    cin >> m;
    Alchemy alc[100];
    for(int i=0;i<m;i++){
      cin >> alc[i].product >> alc[i].num;
      for(int j=0;j<alc[i].num;j++){
	cin >> alc[i].material[j];
      }
    }
    for(int i=0;i<m;i++){
      for(int j=i, k=0;k<m;j++, k++){
	if(j==m) j=0;
	int sum=0;
	for(int l=0;l<alc[j].num;l++){
	  sum+=items[alc[j].material[l]];
	}
	if(sum<items[alc[j].product]) items[alc[j].product]=sum;
      }
    }

    string in;
    cin >> in;
    cout << items[in] << endl;;
  }
  return 0;
}

C++: #include <stdio.h>
#include <string.h>

struct data{
	char name[101];
	int price;
	int ren;
	int renkin[101];
};
data im[101];
int visit[101];

int func(int n){
	int c=im[n].renkin[0];
	int ans=0;
	visit[n]=1;
	if(im[n].renkin[0]==0){
		return im[n].price;
	}
	for(int i=1;i<=c;i++){
		if(visit[im[n].renkin[i]]==1){
			ans+=im[im[n].renkin[i]].price;
		}
		else if(im[im[n].renkin[i]].ren==0){
			ans+=im[im[n].renkin[i]].price;
		}
		else {
			ans+=func(im[n].renkin[i]);
		}
	}
	if(ans<im[n].price)
		im[n].price=ans;
	
	return im[n].price;
}
			

int main(void){
	int n,i,j,l;
	while(scanf("%d",&n),n){
		
		int m,k,t;
		char temp[101];
		for(i=0;i<n;i++){
			scanf("%s %d",im[i].name,&im[i].price);
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			
			scanf("%s",temp);
			for(j=0;j<n;j++){
				if(!(strcmp(im[j].name,temp))){
					im[j].ren=1;
					break;
				}
			}
			t=j;
			scanf("%d",&k);
			im[t].renkin[0]=k;
			for(j=0;j<k;j++){
				scanf("%s",temp);
				for(l=0;l<n;l++){
					if(!(strcmp(im[l].name,temp))){
						im[t].renkin[j+1]=l;
						break;
					}
				}
			}
		}
		scanf("%s",temp);
		for(i=0;i<n;i++){
			if(!(strcmp(im[i].name,temp))){
				break;
			}
		}
		printf("%d\n",func(i));
		
		for(i=0;i<101;i++){
			visit[i]=0;
			im[i].ren=0;
			im[i].price=0;
		}
	}
}

C++: #include <map>
#include <iostream>
#include <vector>
using namespace std;
map<string,int> item;
map<string,vector<string> > rec;

int saiki(string str){
	int ret = 0;
	if(rec[str].size()){
		for(int i=0;i<rec[str].size();i++){
			ret += saiki(rec[str][i]);
		}
	}else{
		ret+=item[str];
	}
	if(item[str]&&item[str]<ret)ret=item[str];
	return ret;
}

int main(){
	int n,m,t;
	string s,rs;
	while(cin >> n && n){
		item.clear();
		rec.clear();
		for(int i=0;i<n;i++){
			cin >>	s;
			cin >> item[s];
		}
		cin >> m;
		for(int i=0;i<m;i++){
			cin >> rs >> t;
			for(int j=0;j<t;j++){
				cin >> s;
				rec[rs].push_back(s);
			}
		}
		cin >> s;
		cout << saiki(s) << endl;
	}
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
using namespace std;
int n,m;
map<string,int> t;
int c[111];
vector<int> v[111];

int solve(int k){
  int sum = 0;
  if(v[k].empty()) return c[k];
  for(int i = 0; i < (int)v[k].size(); i++){
    sum += solve(v[k][i]);
  }
  return min(sum,c[k]);
}

int main(void){
  while(cin >> n && n){
    t.clear();
    for(int i = 0; i < 111; i++) v[i].clear();
    for(int i = 0; i < n; i++){
      string s;
      int a;
      cin >> s >> a;
      t[s] = n;
      c[n] = a;
    }
    cin >> m;
    for(int j = 0; j < m; j++){
      string s;
      int a;
      cin >> s >> a;
      int id = t[s];
      for(int i = 0; i < a; i++){
	cin >> s;
	v[id].push_back(t[s]);
      }
    }
    string s;
    cin >> s;
    cout << solve(t[s]) << endl;
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<vector>
using namespace std;
typedef pair<string, int>P;

int main() {
	int n;
	while (cin >> n, n) {
		vector<P>v;
		for (int i = 0;i < n;i++) {
			string sa;
			int a;
			cin >> sa >> a;
			v.push_back(P(sa, a));
		}

		int m;
		cin >> m;
		for (int i = 0;i < m;i++) {
			string s;
			cin >> s;
			int a;
			cin >> a;

			int it=-1;
			int mny = 0;
			for (int j = 0;j < a;j++) {
				string sb;
				cin >> sb;
				for (int k = 0;k < n;k++) {
					if (sb == v[k].first) {
						mny += v[k].second;
					}
				}
			}
			for (int j = 0;j < v.size();j++) {
				if (s == v[j].first)it = j;
			}
			if (it == -1)v.push_back(P(s, mny));
			else v[it].second = mny;
		}

		string ans;
		cin >> ans;
		for (int i = 0;i < v.size();i++) {
			if (ans == v[i].first) {
				cout << v[i].second << endl;
				break;
			}
		}
	}
	return 0;
}