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++: // 0201.cpp : テ」ツδ。テ」ツつ、テ」ツδウ テ」ツδ療」ツδュテ」ツつクテ」ツつァテ」ツつッテ」ツδ?テ」ツδ陛」ツつ。テ」ツつ、テ」ツδォテ」ツ?ァテ」ツ?凖」ツ??

//#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#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<iostream>
#include<vector>
#include<string>
#include <map>
#include<algorithm>
using namespace std;
int search(string, pair<string, int>*, pair<string, vector<string> >*);
int n;
int m,si;
int main()
{
	
	while (true){
		pair<string, int>p[101];
		pair<string, vector<string> >chan[101];
		cin >> n;
		if (n == 0){
			break;
		}
		for (int i = 0; i < n; i++){
			cin >> p[i].first >> p[i].second;
		}

		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> chan[i].first >> si;
			for (int i2 = 0; i2 < si; i2++){
				string s;
				cin >> s;
				chan[i].second.push_back(s);
			}
		}
		string s;
		cin >> s;
		int ans = search(s,p,chan);
		cout << ans << endl;
	}
}
int search(string s, pair<string, int>*p, pair<string, vector<string> >*chan){
	int num = 0;
	bool flag = true;
	for (int i = 0; i < m; i++){
		if (chan[i].first == s){
			flag = false;
			int o;
	
			for (int i2 = 0; i2 < chan[i].second.size(); i2++){
			for (int i3 = 0; i3 < n; i3++){
				if (p[i3].first == chan[i].second[i2]){
					 o=p[i3].second;
					 break;
				}
			}
				num += min(search(chan[i].second[i2],p,chan),o);
			}
		}
	}
	if (flag){
		for (int i = 0; i < n; i++){
			if (p[i].first == s){
				num += p[i].second;
			}
		}
	}
	return num;
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int a,s[101],d,f[101],g=10000001,h[101],q,w,e;
char z[101][101],x[101][101],c[101][101][101],v[101];
int main(void)
{
	int i,j,k;
	while(1){
		scanf("%d",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			scanf("%s %d",z[i],&s[i]);
		}
		for(i=0;i<a-1;i++){
			for(j=i+1;j<a;j++){
				if(strcmp(z[i],z[j])==0){
					if(s[i]<s[j]) s[j]=s[i];
					else s[i]=s[j];
				}
			}
		}
		scanf("%lld",&d);
		for(i=0;i<d;i++){
			scanf("%s %lld",x[i],&f[i]);
			for(j=0;j<f[i];j++){
				scanf("%s",c[i][j]);
			}
			h[i]=0;
		}
		scanf("%s",v);
		for(i=0;i<a;i++) if(strcmp(v,z[i])==0) if(g>s[i]) g=s[i];
		for(i=0;i<d;i++){
			for(j=0;j<d;j++){
				for(k=0;k<f[j];k++){
					if(strcmp(x[i],c[j][k])==0) h[j]++;
				}
			}
		}
		if(d!=0){
			w=0;
			while(1){
				for(i=0;i<d;i++){
					q=0;
					e=0;
					if(h[i]==0){
						w++;
						for(j=0;j<a;j++){
							for(k=0;k<f[i];k++){
								if(strcmp(z[j],c[i][k])==0){
									q+=s[j];
									break;
								}
							}
						}
						for(j=0;j<a;j++){
							if(strcmp(z[j],x[i])==0){
								if(s[j]>q){
									s[j]=q;
									q=0;
									break;
								}
							}
						}
						if(q!=0){
							s[a]=q;
							strcpy(z[a],x[i]);
							a++;
						}
					}
					h[i]--;
				}
				if(w==d) break;
			}
		}
		for(i=0;i<a;i++){
			if(strcmp(v,z[i])==0){
				printf("%lld\n",s[i]);
				break;
			}
		}
	}
	return 0;
}

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

using namespace std;

map<string,int> minCosts;
map<string,int> value;
map<string,vector<string> > gousei;

int resolveMinCost(string name){
	if(minCosts.find(name)!=minCosts.end())
		return minCosts[name];

	int cost=value[name];
	if(gousei.find(name)!=gousei.end()){
		int sumCost=0;
		for(int i = 0; i < gousei[name].size(); i++){
			sumCost+=resolveMinCost(gousei[name][i]);
		}
		cost=min(cost,sumCost);
	}

	minCosts[name]=cost;
	return cost;
}

int main(){

	int n;
	while(cin>>n&&n!=0){
		for(int i =0; i < n; i++){
			string str;
			cin>>str;
			int val;
			cin>>val;
			value[str]=val;
		}
		int m;
		cin>>m;
		for(int i = 0; i < m; i++){
			string name;
			int k;
			cin>>name;
			cin>>k;
			vector<string> v;
			for(int j = 0; j < k; j++){
				string str;
				cin>>str;
				v.push_back(str);
			}
			gousei[name]=v;
		}
		string item;
		cin>>item;

		cout<<resolveMinCost(item)<<endl;

		gousei.clear();
		value.clear();
		minCosts.clear();
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    while(cin >> n, n){
        int id = 0; map<string, int> M;
        vector<int> dp(n), dp2(n, INF<int>);
        for (int i = 0; i < n; ++i) {
            string s; int x;
            cin >> s >> x;
            dp[id] = x;
            M[s] = id++;
        }
        int m;
        cin >> m;
        vector<int> deg(n);
        vector<vector<int>> G(n);
        for (int i = 0; i < m; ++i) {
            string s; int a;
            cin >> s >> a;
            for (int j = 0; j < a; ++j) {
                string t; cin >> t;
                G[M[t]].emplace_back(M[s]);
                deg[M[s]]++;
            }
        }
        queue<int> Q;
        for (int i = 0; i < n; ++i) {
            if(!deg[i]) Q.emplace(i);
        }
        while(!Q.empty()){
            int i = Q.front(); Q.pop();
            for (auto &&j : G[i]) {
                if(dp2[j] == INF<int>) dp2[j] = min(dp[i], dp2[i]);
                else dp2[j] += min(dp[i], dp2[i]);
                deg[j]--;
                if(!deg[j]) Q.emplace(j);
            }
        }
        string s;
        cin >> s;
        cout << min(dp[M[s]], dp2[M[s]]) << "\n";
    }
    return 0;
}