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<stdio.h>
#include<list>
#include<string.h>

class item{
 public:
  int pr;
  char name[101];
  std::list<int> ren;
};
item its[100];
int solve(int n){
  int sum;
  std::list<int>::iterator it;
  if(its[n].ren.empty()){
    return its[n].pr;
  }
  for(sum=0,it=its[n].ren.begin();it!=its[n].ren.end();it++){
    sum+=solve(*it);
    if(its[n].pr<sum) return its[n].pr;
  }
  return sum;
}
int main(){
  int i,j,n,m,k,l,o;
  char buf[400];
  using namespace std;
  list<int>::iterator it;
  do{
    scanf("%d",&n);
    if(!n) break;
    for(i=0;i<n;i++) scanf("%s %d",its[i].name,&(its[i].pr));
    scanf("%d",&m);
    for(i=0;i<m;i++){
      scanf("%s ",buf);
      for(j=0;j<n;j++) if(!strcmp(buf,its[j].name)) break;
      scanf("%d ",&k);
      for(l=0;l<k-1;l++){
	scanf("%s ",buf);
	for(o=0;o<n;o++) if(!strcmp(its[o].name,buf)) break;
	its[j].ren.push_back(o);
      }
      scanf("%s",buf);
      for(o=0;o<n;o++) if(!strcmp(its[o].name,buf)) break;
      its[j].ren.push_back(o);
    }
    scanf("%s",buf);
    for(i=0;i<n;i++) if(!strcmp(buf,its[i].name)) break;
    printf("%d\n",solve(i));
  }while(1);
  return 0;
}

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


using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)



class Rec{
public:
	int prodI;
	vector<int> ingIS;
};


int main(){
	while(true){
		int itemL;
		cin >> itemL;					if(!itemL)break;
		map<string,int> itemX2I;
		map<int,string> itemI2X;
		vector<int> priceS;

		REP(itemI,itemL){
			string itemX;
			int itemPrice;
			cin >> itemX >> itemPrice;
			itemX2I[itemX] = itemI;
			itemI2X[itemI] = itemX;
			priceS.push_back(itemPrice);
		}

		int recL;
		cin >> recL;
		vector<Rec> recS;

		REP(recI,recL){
			Rec rec;
			string prodX;
			cin >> prodX;
			rec.prodI = itemX2I[prodX];
			int ingL;
			cin >> ingL;
			REP(gomi,ingL){
				string ingX;
				cin >> ingX;
				rec.ingIS.push_back(itemX2I[ingX]);
			}
			recS.push_back(rec);
		}

		REP(gomi,recL+1){
			REP(recI,recL){
				Rec rec = recS[recI];
				int prodI = rec.prodI;
				int nPrice = 0;
				REP(ingII,rec.ingIS.size()){
					int ingI = rec.ingIS[ingII];
					nPrice += priceS[ingI];
				}
				priceS[prodI] = min(priceS[prodI],nPrice);
			}
		}

		string prodX;
		cin >> prodX;
		int prodI = itemX2I[prodX];
		int price = priceS[prodI];
		cout << price << endl;


	}
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)



map<string,int> sina;
vector<int> haigo[100];
int val[100];

int saiki(int a){
	if(haigo[a].size()==0)return val[a];
	
	int sum=0;
	for(int i=0;i<haigo[a].size();i++){
		sum += saiki(haigo[a][i]);
	}
	
	return min(sum,val[a]);
}

int main(){
	while(1){
		int n;
		cin>>n;
		
		if(n==0)break;
		
		sina.clear();
		
		rep(i,n){
			string a;
			int b;
			
			cin>>a>>b;
			sina[a]=i;
			val[i]=b;
		}
		
		int k;
		cin>>k;
		
		rep(i,100)haigo[i].clear();
		
		rep(i,k){
			string a;
			int b;
			cin>>a>>b;
			
			rep(j,b){
				string c;
				cin>>c;
				
				haigo[sina[a]].push_back(sina[c]);
			}
		}
		
		string d;
		cin>>d;
		
		printf("%d\n",saiki(sina[d]));
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int n;
map<string,int>M;
map<string,vector<string> >A;
int f(string s)
{
    int now=M[s];
    if(A[s].size()>0)
    {
        int a=0;
        for(int i=0;i<A[s].size();i++)a+=f(A[s][i]);
        now=min(now,a);
    }
    return now;
}
main()
{
	while(cin>>n,n)
	{
		M.clear();
		A.clear();
		for(int i=0;i<n;i++)
		{
			string s;int a;cin>>s>>a;M[s]=a;
		}
		int m;cin>>m;
		for(int i=0;i<m;i++)
		{
			string s;cin>>s;
			int k;cin>>k;
			for(;k--;)
			{
				string t;cin>>t;
				A[s].push_back(t);
			}
		}
		string s;cin>>s;
		cout<<f(s)<<endl;
	}
}

C++: #include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef map<string,int> msi;
const int INF=2<<29;
msi p;
int n,m,price[100];
vector<vector<int> > recipe[100];
int search(int x,bool used[100]){
	int minim=price[x];
	if(used[x])
	return INF;
	else
	used[x]=true;
	for(int i=0;i<recipe[x].size();i++){
		int sum=0;
		for(int j=0;j<recipe[x][i].size();j++)
			sum+=search(recipe[x][i][j],used);
		minim=min(minim,sum);
	}
	used[x]=false;
	return minim;
		
}
int main() {
	string s,sb;
	while(cin >> n && n!=0){
		for(int i=0;i<n;i++){
			cin >> s >> price[i];
			p.insert(msi::value_type(s,i));
		}
		cin >> m;
		int num;
		for(int i=0;i<m;i++){
			vector<int> material;
			cin >> s >> num;
			for(int j=0;j<num;j++){
			cin >> sb; 
			material.push_back(p[sb]);
			}
			recipe[p[s]].push_back(material);
		}
		cin >> s;
		bool used[100]={};
		cout << search(p[s],used) << endl;
		for(int i=0;i<100;i++){
			recipe[i].clear();
			price[i]=0;
		}
	}
	// your code goes here
	return 0;
}

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

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

using namespace std;

map<string,int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x){
    if(recipe[x].size() == 0){
        return price[x];
    }

    int ans=0;
    rep(i,recipe[x].size()){
        ans += min(solve(recipe[x][i]), price[recipe[x][i]]);
    }
    return ans;
}

int main(){
    int n;
    while(cin >> n,n){
        rep(i,n){
            string name;
            int p;
            cin >> name >> p;
            name_id[name] = i;
            price[i] = p;
        }

        int m;
        cin >> m;
        rep(i,m){
            string name;
            int num;
            cin >> name >> num;
            int id = name_id[name];
            rep(k,num){
                string r;
                cin >> r;
                recipe[id].push_back(name_id[r]);
            }
        }

        string a;
        cin >> a;
        cout << solve(name_id[a]) << endl;
    }
    return 0;
}