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 <set>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
int n,m,k;string s;
map<string,int> cost;
map<string,vector<string>> recipe;
int dfs(string t){
	int ans=0;
	vector<string> vs=recipe[t];
	if(!vs.empty()){
		for(auto it=vs.begin();it<vs.end();it++){ans+=dfs(vs.at(it-vs.begin()));}
		cost[t]=min(cost[t],ans);
		return cost[t];
	}else{
		return cost[t];
	}
	
}
int main()
{
	while(cin>>n&&n){
		cost.clear();recipe.clear();
		for(int i=0;i<n;i++){
			int tmp;
			cin>>s>>tmp;
			cost[s]=tmp;
		}
		cin>>m;
		for(int i=0;i<m;i++){
			vector<string> vs;vs.clear();
			string t;cin>>s>>k;
			while(k--){cin>>t;vs.push_back(t);}
			recipe[s]=vs;
		}
		cin>>s;
		cout<<dfs(s)<<endl;
	}
    return 0;
}

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

map<string, vector<string> > mix;
map<string, int> prise;
map<string, int> min_prise;

int cal_min(string s)
{
  if(min_prise.find(s) != min_prise.end())
    return min_prise[s];
  if(mix.find(s) == mix.end())
    return prise[s];
  int p = 0;
  for(vector<string>::iterator i = mix[s].begin(); i != mix[s].end(); i++)
  {
    p += cal_min(*i);
    if(p >= prise[s])
    {
      min_prise.insert(pair<string, int>(s, prise[s]));
      return min_prise[s];
    }
  }
  min_prise.insert(pair<string, int>(s, p));
  return p;
}

int main()
{
  int n, m;
  while(1)
  {
    cin >> n;
    if(!n)
      break;
    for(int i = 0; i < n; i++)
    {
      string s;
      int p;
      cin >> s >> p;
      prise.insert(pair<string, int>(s, p));
    }
    cin >> m;

    for(int i = 0; i < m; i++)
    {
      string item;
      int d;
      vector<string> v;
      cin >> item >> d;
      for(int j = 0; j < d; j++)
      {
        string s;
        cin >> s;
        v.push_back(s);
      }
      mix.insert(pair<string, vector<string> >(item, v));
    }

    string str;
    cin >> str;
    cout << cal_min(str) << endl;
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int func(string name,  map<string,int> &list,  map<string,vector<string> > &renkin){
  if(renkin.find(name)==renkin.end()) return list[name];
  int ret = 0;
  vector<string> mat = renkin[name];
  REP(i,mat.size()){
    ret += min(list[mat[i]], func(mat[i],list,renkin));
  }
  return ret;
}

int main(void){
  int n,m;
  while(cin>>n){
    if(n==0) break;
    map<string,int> list;
    map<string,vector<string> > renkin;
    REP(j,n){
      string i;
      int p;
      cin>>i>>p;
      list[i] = p;
    }
    cin>>m;
    REP(i,m){
      string o;
      int k;
      cin>>o>>k;
      vector<string> q(k);
      REP(j,k) cin>>q[j];
      renkin[o] = q;
    }
    string item;
    cin>>item;
    cout<<func(item,list,renkin)<<endl;
  }
  return 0;
}

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;

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(cin>>n,n){
		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>>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++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;
map<string,int> dic;
map<string,bool> isp;
map< string, vector<string> > gm;

int solve(string s){
    int r = dic[s];
    int t = 0;
    if(gm[s].size()){
	REP(i,gm[s].size()){
	    t += solve(gm[s][i]);
	}
    }else{
	return dic[s];
    }
    r = min(r,t);
    return r;
}

int main(){
    while(true){
	int n;
	cin >> n;
	if(n==0) break;
	REP(i,n){
	    string s;
	    int p;
	    cin >> s >> p;
	    dic[s] = p;
	    isp[s] = false;
	    gm[s] = vector<string>(0);
	}
	cin >> n;
	REP(i,n){
	    string s;
	    int m;
	    cin >> s >> m;
	    REP(j,m){
		string temp;
		cin >> temp;
		isp[temp] = true;
		gm[s].PB(temp);
	    }
	}
	string s;
	cin >> s;
	cout << solve(s) << endl;
    }
    return 0;
}