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 <bits/stdc++.h>

using namespace std;

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

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
	while (cin >> N, N) {
		REP(i, N) cin >> S[i] >> P[i];
		cin >> M;
		REP(i, M) {
			cin >> O[i] >> K[i];
			REP(j, K[i]) cin >> Q[i][j];
		}
		cin >> T;
		
		map<string, int> conv;
		REP(i, N) conv[S[i]] = conv.size() - 1;
		
		REP(i, M) g[i].clear();
		REP(i, M) {
			g[i].push_back(conv[O[i]]);
			REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
		}
		
		fill(cost, cost + 200, INF);
		REP(i, N) chmin(cost[conv[S[i]]], P[i]);
		
		REP(t, M - 1) {
			REP(i, M) {
				ll sum = 0;
				FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
				chmin(cost[g[i][0]], sum);
			}
		}
		
		cout << cost[conv[T]] << endl;
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		map<string, long long int> mp;
		string a;
		long long int b;
		for (int i = 0; i < n; i++) {
			cin >> a >> b;
			mp[a] = b;
		}
		int m;
		cin >> m;
		vector<string> data[105];
		string g;
		for (int i = 0; i < m; i++) {
			cin >> g;
			data[i].push_back(g);
			int w;
			cin >> w;
			for (int j = 0; j < w; j++) {
				cin >> g;
				data[i].push_back(g);
			}
		}
		bool hantei = true;
		while (hantei) {
			hantei = false;
			for (int i = 0; i < m; i++) {
				long long int p = 0;
				for (int j = 1; j < data[i].size(); j++) {
					p += mp[data[i][j]];
				}
				if (mp[data[i][0]] > p) {
					mp[data[i][0]] = p;
					hantei = true;
				}
			}
		}
		string h;
		cin >> h;
		cout << mp[h] << endl;
	}
	return 0;
}

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

int main(){
	int n , m , k , p , psum;
	string a , b;
	string want;
	while(cin >> n , n){
		map<string , int> item;
		for(int i=0;i<n;++i){
			cin >> a >> p ;
			item.insert(map<string , int>::value_type( a , p ));
		}
		cin >> m;
		for(int i=0;i<m;++i){
			psum=0;
			cin >> a >> k; //??¬??????a ???????????°k
			for(int j=0 ; j<k ;++j){
				cin >> b;  //??¬??????
				if(item.find(b)!=item.end()) psum+=item[b];
			}
				if(psum < item[a]) {
					item[a]=psum;//???????????´
				}
		}
		cin >> want;
		cout << item[want] << endl;
	}	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>
#include <map>

using namespace std;
using ll = long long;

const ll INF = 1<<30;
const ll MOD = (ll)1e9 + 7;
const ll MAX_N = (ll)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

ll N, M;
map<string, ll> price;
map<string, vector<string>> G;

ll dfs(const string &target)
{
    if(G.count(target) == 0) return price[target];
    ll res = 0;
    for(auto &&x : G[target]) res += dfs(x);
    return min(res, price[target]);
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> N, N)
    {
        price.clear();
        G.clear();
        for(ll i = 0; i < N; i++)
        {
            string s; ll p; cin >> s >> p;
            price[s] = p;
        }
        cin >> M;
        for(ll i = 0; i < M; i++)
        {
            string s; ll k; cin >> s >> k;
            for(ll j = 0; j < k; j++)
            {
                string q; cin >> q;
                G[s].push_back(q);
            }
        }
        string target; cin >> target;
        cout << dfs(target) << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <string.h>

int resipi[101][101],item_price[101],visit[101];
char item_name[101][101];

int func(int num){
	int i,j;
	int ans=0;
	if(resipi[num][0]==0)
		return item_price[num];
	
	for(i=1;i<=resipi[num][0];i++){
		
		if(visit[resipi[num][i]]==1){
			ans+=item_price[resipi[num][i]];
		}
		else if(resipi[resipi[num][i]][0]==0){
			visit[resipi[num][i]]=1;
			ans+=item_price[resipi[num][i]];
		}
		else{
			visit[resipi[num][i]]=1;
			ans+=func(resipi[num][i]);
		}
	}
	if(ans<item_price[num])item_price[num]=ans;
	return item_price[num];
}


int main(void){
	int n;
	while(scanf("%d",&n),n){
		int i,j,l,m,k,t;
		char name[101];
		
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%s%d",item_name[i],&item_price[i]);
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			scanf("%s%d",name,&k);
			for(j=0;j<n;j++){
				if(!(strcmp(item_name[j],name))){
					t=j;
					resipi[t][0]=k;
				}
			}
			for(j=0;j<k;j++){
				scanf("%s",name);
				for(l=0;l<n;l++){
					if(!(strcmp(item_name[l],name))){
						resipi[t][j+1]=l;
						break;
					}
				}
			}
		}
		scanf("%s",name);
		for(i=0;i<n;i++){
			if(!(strcmp(item_name[i],name))){
				break;
			}
		}
		printf("%d\n",func(i));
		
		for(i=0;i<101;i++){
			resipi[i][0]=0;
			visit[i]=0;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <map>
#include <vector>
using namespace std;
typedef map<string, int> m;

m price;
map<string,vector<string> > renkin;
string p[100];

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

    cin >> m;
    for(int i=0;i<m;i++) {
      int sozai;
      cin >> p[i] >> sozai;
      for(int k=0;k<sozai;k++) {
        string name;
        cin >> name;
        renkin[p[i]].push_back(name);
      }
    }

    bool update = true;
    while(update) {
      update = false;
      for(int i=0;i<m;i++) {
        int sum = 0;
        for(int j=0;j<renkin[p[i]].size();j++) {
          sum += price[renkin[p[i]][j]];
        }
        if(sum < price[p[i]]) {
          price[p[i]] = sum;
          update = true;
        }
      }
    }
    string str;
    cin >> str;
    cout << price[str] << endl;
    price.clear();
    renkin.clear();
  }
}