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<vector>
#include<map>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n),n){
        map<string,int>mi;
        for(int i=0;i<n;i++){
            string str;
            int val;
            cin>>str>>val;
            mi[str]=val;
        }
        map<string,vector<string> >es;
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            string t;
            int c;
            cin>>t>>c;
            vector<string>V(c);
            for(int i=0;i<c;i++)cin>>V[i];
            es[t]=V;
        }

        for(int i=0;i<n;i++){
            map<string,vector<string> >::iterator it;
            for(it=es.begin();it!=es.end();it++){
                string s=it->first;
                vector<string>V=it->second;
                int sum=0;
                for(int j=0;j<V.size();j++){
                    sum+=mi[V[j]];
                }
                mi[s]=min(mi[s],sum);
            }
        }
        string str;
        cin>>str;
        printf("%d\n",mi[str]);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

ll n;
map<string, ll> msi;
map<string, vector<string>> msvs;
ll m;
string t;

ll solve(string s){
	if (msvs.find(s) == msvs.end()){
		return msi[s];
	}
	else{
		ll res = 0;
		for (vector<string>::iterator it = msvs[s].begin(); it != msvs[s].end(); it++){
			res += solve(*it);
		}
		return min(msi[s], res);
	}
}

int main(){
	while (cin >> n&&n){
		msi.clear();
		msvs.clear();
		REP(i, n){
			string s;
			ll p;
			cin >> s >> p;
			msi[s] = p;
		}
		cin >> m;
		REP(i, m){
			string o;
			ll k;
			cin >> o >> k;
			REP(j, k){
				string q;
				cin >> q;
				msvs[o].push_back(q);
			}
		}
		cin >> t;
		cout << solve(t) << endl;
	}
}

C++: 
 
#include <iostream>
#include <string>
#include <map>
using namespace std;
 
int main(){
  int n,m,tmp2,sum;
  string tmp,make,fa;
  while(cin >> n,n){
    sum = 0;
    map <string,int> item;
    while(n--){
      cin >> tmp >> tmp2;
      item.insert(map<string,int>::value_type(tmp,tmp2));
    }
    cin >> m;
    while(m--){
      cin >> tmp >> tmp2;
      sum = 0;
      while(tmp2--){
    cin >> fa;
    if(item.find(fa)!=item.end()) sum += item[fa];
      }
      if(sum < item[tmp]) item[tmp] = sum;
    }
    cin >> make;
    cout << item[make] << endl;
  }
}

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

using namespace std;

map<string,int> s1;
map<string,vector<string>> s2;
int INF=100000000;
int gold(string s){
	int res=0;
	int res2=INF;
	res=s1[s];
	if(s2[s].size()>0)res2=0;
	for(int i=0;i<s2[s].size();i++){
		res2+=gold(s2[s][i]);
	}
	res=min(res,res2);
	return res;
}

int main(void){
	int n,m;
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		s1.clear();
		s2.clear();
		for(int i=0;i<n;i++){
			string str;
			int c;
			cin >> str >> c;
			s1[str]=c;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			string str;
			int k;
			cin >> str >> k;
			for(int j=0;j<k;j++){
				string s;
				cin >> s;
				s2[str].push_back(s);
			}
		}
		string s3;
		cin >> s3;
		printf("%d\n",gold(s3));
	}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

map<string,int> table;
map<string,vector<string>> recipe;
map<string,int> opt;

int dfs(string& name,map<string,int>& state){
    state[name]=1;
    int& res=opt[name];
    res=table[name];
    if(recipe[name].empty()) return res;

    int tmp=0;
    for(auto &next:recipe[name]){
        if(state[next]==1) tmp+=table[next];
        else if(state[next]==2) tmp+=opt[next];
        else tmp+=dfs(next,state);
    }
    res=min(res,tmp);

    state[name]=2;
    return res;
}

void solve(){
    int n;
    while(true){
        cin >> n;
        if(!n) break;

        table.clear();
        recipe.clear();
        opt.clear();

        rep(i,0,n){
            string name;
            int cost;
            cin >> name >> cost;
            table[name]=cost;
            opt[name]=inf;
        }

        int m;
        cin >> m;
        rep(i,0,m){
            string name1;
            int k;
            cin >> name1 >> k;
            rep(j,0,k){
                string name2;
                cin >> name2;
                recipe[name1].push_back(name2);
            }
        }

        string str;
        cin >> str;
        map<string,int> state;
        cout << dfs(str,state) << endl;
    }
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}