Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"log"
	"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))
		log.Printf("%+v\n", 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<map>
#include<cassert>
#include<vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int,bool> P;
map<string,P> material;
vector<string> vec[110];
map<string,P> index;


int rec(string target){
  
  int value = MAX;
  map<string,P>::iterator it = material.begin();
  while(it != material.end() ){
    if((*it).F == target)value = min(value,(*it).S.F);
    it++;
  }
  
  int X = 10000000;
  int id = -1,pre=X;
  if(index[target].S)id = index[target].F;

  if(id != -1){
    for(int i=0;i<vec[id].size();i++){
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1?X:pre-X,value);
  
  return value;
}


int main(){
  int n,m;

  while(true){
    cin >> n;
    if(n == 0)break;
    
    material.clear();
    for(int i=0;i<n;i++){
      vec[i].clear();
      string s; int val;
      cin >> s >> val;
    
      material[s] = P(val,true);
    }
   
    index.clear();
    cin >> m;
    for(int i=0;i<m;i++){
      string s,ss; int cn;
      cin >> s >> cn;
      index[s] = P(i,true);
      for(int j=0;j<cn;j++){
	cin >> ss;
	vec[i].push_back(ss);
      }
    }
    
    string st;
    cin >> st;
    cout << rec(st) << endl;


  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<cassert>
#include<vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int,bool> P;
map<string,P> material;
vector<string> vec[110];
map<string,P> index;


int rec(string target){
  
  int value = MAX;
  map<string,P>::iterator it = material.begin();
  while(it != material.end() ){
    if((*it).F == target)value = min(value,(*it).S.F);
    it++;
  }
  
  int X = 10000000;
  int id = -1,pre=X;
  if(index[target].S)id = index[target].F;

  if(id != -1){
    for(int i=0;i<vec[id].size();i++){
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1?X:pre-X,value);
  assert(value != MAX);
  return value;
}


int main(){
  int n,m;

  while(true){
    cin >> n;
    if(n == 0)break;
    
    material.clear();
    for(int i=0;i<n;i++){
      vec[i].clear();
      string s; int val;
      cin >> s >> val;
    
      material[s] = P(val,true);
    }
   
    index.clear();
    cin >> m;
    for(int i=0;i<m;i++){
      string s,ss; int cn;
      cin >> s >> cn;
      index[s] = P(i,true);
      for(int j=0;j<cn;j++){
	cin >> ss;
	vec[i].push_back(ss);
      }
    }
    
    string st;
    cin >> st;
    cout << rec(st) << endl;


  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
using namespace std;

struct lst {
	int n;
	string it[101];
};
lst ls[101];
map<string,int> co,ln;

int ren(string st) {
	int i,k,m,s=0;
	k=ln[st]; m=ls[k].n;
	if (m==0) return co[st];
	for (i=0;i<m;i++) s+=ren(ls[k].it[i]);
	if (s>co[st]) s=co[st]; 
	return s;
	 }

int main() {
	int i,j,k,n,m,c;
	string st;
	while(cin >> n) {
		if (n==0) break;
		for (i=0;i<n;i++) { cin >> st >> c; co[st]=c; ln[st]=i;}
		cin >> n;
		for (i=0;i<n;i++) {
			cin >> st; k=ln[st];
			cin >> m; ls[k].n=m;
			if (m!=0) for (j=0;j<m;j++) cin >> ls[k].it[j];
		}
		cin >> st;
		k=ren(st);
		cout << k << endl;
	}	  
	return 0;
}

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,k){
			haigo[i].clear();
			
			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<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s[101],d,f[101],g=1000001,h[101],j,i,k,q,w,e;
	char z[101][101],x[101][101],c[101][101][101],v[101];
	while(1){
		scanf("%d",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			scanf("%s %d",z[i],&s[i]);
		}
		scanf("%d",&d);
		for(i=0;i<d;i++){
			scanf("%s %d",x[i],&f[i]);
			for(j=0;j<f[i];j++){
				scanf("%s",c[i][j]);
			}
		}
		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){
			while(1){
				for(i=0;i<d;i++){
					q=0;
					w=0;
					e=0;
					if(h[i]==0){
						for(j=0;j<a;j++){
							for(k=0;k<f[i];k++){
								if(strcmp(z[j],c[i][k])==0){
									q+=s[j];
								//	printf("%d\n",q);
								}
							}
						}
						for(j=0;j<a;j++){
							if(strcmp(z[j],x[i])==0){
								if(s[j]>q){
								//	printf("%d %d\n",i,j);
									s[j]=q;
									q=0;
									break;
								}
							}
						}
					}
					h[i]--;
				}
				for(i=0;i<d;i++){
					if(h[i]<0) w++;
				}
				if(w==d) break;
			}
		}
		for(i=0;i<a;i++){
			if(strcmp(v,z[i])==0){
				printf("%d\n",s[i]);
				break;
			}
		}
	}
	return 0;
}

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]=ret; 
}

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;
}