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 <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <sstream>
using namespace std;
typedef long long ll;

map<string, int> item;
map<string, vector<string> > recipe;
map<string, int> dp;

int solve(string s){
    if(dp.count(s))return dp[s];
    
    int res = 1 << 29;
    if(item.count(s)){
        res = min(res, item[s]);
    }
    
    if(recipe.count(s)){
        int total = 0;
        
        vector<string> &grp = recipe[s];
        for(int i=0;i<grp.size();i++)total += solve(grp[i]);
        
        res = min(res, total);
    }
    
    return dp[s] = res;
}

int main(){
    int N, M;
    while(cin >> N && N){
        item.clear();
        recipe.clear();
        dp.clear();
        
        for(int i=0;i<N;i++){
            string name;
            int price;
            
            cin >> name >> price;
            item[name] = price;
        }
        
        cin >> M;
        for(int i=0;i<M;i++){
            string name;
            int k;
            
            cin >> name >> k;
            vector<string> grp(k);
            
            for(int j=0;j<k;j++)cin >> grp[j];
            
            recipe[name] = grp;
        }
        
        string target;
        cin >> target;
        
        cout << solve(target) << endl;
    }
}

C++: #include<iostream>
#include<list>
#include<string>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define foreach(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
using namespace std;
int main(){
	int n,m,k,f,c; string b1,b2;
	while(cin>>n,n){
		map<string,int> cost;
		map<string,list<string> > recipe;
		rep(i,n)cin>>b1>>c,cost[b1]=c;
		cin>>m; rep(i,m){
			cin>>b1>>k;
			list<string> r;
			rep(j,k)cin>>b2,r.push_back(b2);
			recipe[b1]=r;
		}
		f=0; __typeof(recipe.begin()) p;
		while(!f){
			f=1;
			foreach(i,cost)if((p=recipe.find(i->first))!=recipe.end()){
				c=0;
				foreach(j,p->second)c+=cost[*j];
				if(c<i->second)i->second=c,f=0;
			}
		}
		cin>>b1;
		cout<<cost[b1]<<endl;
	}
	return 0;
}

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

int dfs(string t, map<string, int> cost, map<string, vector<string>> recipe) {
    int res = 0;
    
    if (recipe.find(t)!=recipe.end()) {
        for (auto& qi : recipe[t]) {
            res += dfs(qi, cost, recipe);
        }
        
        return min(res, cost[t]);
    }
    else {
        return cost[t];
    }
}

signed main() {
    while (true) {
        int n;
        cin >> n;
        
        if (n==0) {
            break;
        }
        
        map<string, int> cost;
        
        for (int i=0; i<n; i++) {
            string si;
            int pi;
            cin >> si >> pi;
            cost[si] = pi;
        }
        
        int m;
        cin >> m;
        
        map<string, vector<string>> recipe;
        
        for (int i=0; i<m; i++) {
            string oi;
            int ki;
            cin >> oi >> ki;
            
            for (int j=0; j<ki; j++) {
                string qi;
                cin >> qi;
                recipe[oi].push_back(qi);
            }
        }
        
        string t;
        cin >> t;
        cout << dfs(t, cost, recipe) << endl;
    }
}

C++: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int dp[110], price[110];
int solve(int id, vector< vector< vector<int> > >& recipe) {
    if(dp[id] != INF) return dp[id];

    dp[id] = price[id];
    for(auto vec : recipe[id]) {
        int sum = 0;
        for(auto e : vec) {
            sum += solve(e, recipe);
        }
        chmin(dp[id], sum);
    }
    return dp[id];
}
 
signed main() {
    int N, M;
    while(cin >> N, N) {
        int id = 0;
        map<string, int> stoid;

        for(int i=0; i<N; i++) {
            string s;
            cin >> s >> price[i];
            stoid[s] = id++;
        }

        cin >> M;
        fill(dp, dp + N, INF);
        vector< vector< vector<int> > > recipe(N);
        for(int i=0; i<M; i++) {
            string s; cin >> s;
            int idx = stoid[s];

            int K; cin >> K;
            vector<int> tmp_list;
            for(int j=0; j<K; j++) {
                string t; cin >> t;
                tmp_list.push_back(stoid[t]);
            }
            recipe[idx].push_back(tmp_list);
        }

        string x; cin >> x;
        cout << solve(stoid[x], recipe) << endl;
    }
    return 0;
}