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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
map<string,int> data;
map<string,vector<string> >ren;

inline int res(string s){
	if(!ren.count(s))return data[s];
	int sum = 0;
	for(int i =0;i <ren[s].size();i++){
		sum +=res(ren[s][i]);
	}
	if(data[s]>sum){
		data[s]=sum;
	}
	return data[s];
}

int main(){
	int n,m;
	while(scanf("%d",&n),n){
		data.clear();
		ren.clear();
		for(int i =0;i < n;i++){
			string item;
			int cst;
			cin>>item>>cst;
			data[item] =cst;
		}
		cin>>m;
		int k;
		for(int i =0;i <m;i++){
			string renkin;
			cin>>renkin;
			scanf("%d",&k);
			//int sum=0;
			for(int j =0;j <k;j++){
				string tmp;
				cin>>tmp;
				ren[renkin].push_back(tmp);
			}
			//data[renkin]=min(sum,data[renkin]);
		}
		string ans;
		cin>>ans;
		cout <<res(ans)<<endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
#include<map>
#include<string>
using namespace std;

int N, M, P[100], K;
int memo[100];

int DFS(int idx, const vector<int> e[100]){
	if(memo[idx] != INT_MAX) return memo[idx];
	memo[idx] = P[idx];
	if( !e[idx].empty() ){
		int cost = 0;
		for(int i = 0; i < e[idx].size(); i++){
			cost += DFS(e[idx][i], e);
		}
		memo[idx] = min(memo[idx], cost);
	}
	return memo[idx];
}

int main(){
	while(cin >> N, N){
		vector<int> e[100];
		map<string, int> idx;
		string I, O, Q;
		fill_n(memo, 100, INT_MAX);
	
		for(int i = 0; i < N; i++){
			cin >> I >> P[i];
			idx[I] = i;
		}
		cin >> M;
		for(int i = 0; i < M; i++){
			cin >> O >> K;
			int O_idx = idx[O];
			for(int j = 0; j < K; j++){
				cin >> Q;
				e[O_idx].push_back(idx[Q]);
			}
		}
		cin >> I;
		printf("%d\n", DFS(idx[I], e) );
	}
	return 0;
}

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

using namespace std;

class recipe{
public:
	vector<string> src;
	string dst;
	int num;
};

int main(void){
	int n;
	
	while(cin >> n && n){
		int k;
		map<string,int> m;
		vector<recipe> rs;
		string str;
		
		int tmp;
		for(int i=0;i<n;i++){
			cin >> str >> tmp;
			m[str] = tmp;
		}
		cin >> n;
		for(int i=0;i<n;i++){
			recipe r;
			cin >> str;
			r.dst = str;
			cin >> k;
			r.num = k;
			for(int i=0;i<k;i++){
				cin >> str;
				r.src.push_back(str);
			}
			rs.push_back(r);
		}
		
		bool ended = false;
		while(!ended){
			ended = true;
			for(int i=0;i<rs.size();i++){
				tmp = 0;
				for(int j=0;j<rs[i].num;j++){
					tmp += m[rs[i].src[j]];
				}
				if(m[rs[i].dst] > tmp){
					ended = false;
					m[rs[i].dst] = tmp;
				}
			}
		}
		cin >> str;
		cout << m[str] << endl;
	}
}

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

using namespace std;

int minprice(string name, map<string,vector<string> > recipes, map<string,int> price){
	int p = price[name];
	map<string,vector<string> >::iterator recipe;
	if((recipe = recipes.find(name)) != recipes.end()){
		int tmp = 0;
		vector<string> materials = recipe->second;
		for(vector<string>::iterator material = materials.begin(); material != materials.end(); ++material){
			tmp += minprice(*material, recipes, price);
		}
		if(p > tmp){
			p = tmp;
		}
	}
	return p;
}

int main(){
	int n;
	cin >> n;
	while(n != 0){
		map<string,int> items;
		for(int i = 0; i < n; ++i){
			string name;
			int price;
			cin >> name >> price;
			items[name] = price;
		}
		int m;
		cin >> m;
		map<string,vector<string> > recipes;
		for(int i = 0; i < m; ++i){
			string name;
			int k;
			cin >> name >> k;
			for(int j = 0; j < k; ++j){
				string material;
				cin >> material;
				recipes[name].push_back(material);
			}
		}
		string item;
		cin >> item;
		cout << minprice(item, recipes, items) << endl;
		cin >> n;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
using namespace std;
typedef struct{
  vector<string> sozai;
  int money;
}BUTU;
pair<string,int> P[1002];
BUTU butu[1024];
int n,p,m,a,b;
string s,ss,mokuhyou;
int tansaku(string now){
  for(int i=0;i<n;i++){
    if(P[i].first==now){
      return P[i].second;
    }
  }
  return -1;
}
int check(string now){
  int banti=tansaku(now);
  int f=butu[banti].sozai.size();
  int cnt=0;
  if(f==0){
    return butu[banti].money;
  }
  else{
    for(int i=0;i<f;i++){
      if(tansaku(butu[banti].sozai[i])!=-1){
	cnt+=check(butu[banti].sozai[i]);
      }
      else{
	return butu[banti].money;
      }
    }
    return min(cnt,butu[banti].money);
  }
  return -1;
}
main(){
  while(1){
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++){
      cin>>s>>p;
      butu[i].money=p;
      P[i].first=s;
      P[i].second=i;
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>s>>a;
      b=tansaku(s);
      for(int j=0;j<a;j++){
	cin>>ss;
	butu[b].sozai.push_back(ss);
      }
    }
    cin>>mokuhyou;
    cout<<check(mokuhyou)<<endl;
  }
}