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 <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(scanf("%d",&n),n){
		data.clear();
		ren.clear();
		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;
			scanf("%d",&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 <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

map<string, vector<string> > recipe;
map<string, int> cost;

int search(string item){
    if(recipe[item].size() == 0) return cost[item];
    else {
        int sum = 0;
        for(int i=0; i<recipe[item].size(); i++){
            sum += search(recipe[item][i]);
        }
        return min(cost[item], sum);
    }
}

int main(){
    int n;
    while(cin >> n, n){
        for(int i=0; i< n; i++){
            string name; int m;
            cin >> name >> m;
            cost[name] = m;
        }
        cin >> n;
        for(int i=0; i< n; i++){
            string item;
            cin >> item;
            int m;
            cin >> m;
            for(int j=0; j < m; j++){
                string material;
                cin >> material;
                recipe[item].push_back(material);
            }
        }
        string item;
        cin >> item;
        cout << search(item) << endl;
    }
}

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

map<string, int> v;
map<string, vector<string> > l;

int main()
{
	int n;
	while (scanf("%d", &n), n){
		for (int i = 0; i < n; i++){
			string str;
			int p;
			cin >> str >> p;
			v[str] = p;
		}
		
		int m;
		scanf("%d", &m);
		for (int i = 0; i < m; i++){
			string o;
			int k;
			cin >> o >> k;
			for (int j = 0; j < k; j++){
				string q;
				cin >> q;
				l[o].push_back(q);
			}
		}
		
		for (int k = 0; k < 1000; k++){
		//while (true){
			bool update = false;
			
			typedef map<string, vector<string> >::iterator ite;
			for (ite i = l.begin(); i != l.end(); ++i){
				int sum = 0;
				for (int j = 0; j < (*i).second.size(); j++){
					sum += v[(*i).second[j]];
				}
				if (v[(*i).first] > sum){
					v[(*i).first] = sum;
					update = true;
				}
			}
			
		//	if (!update) break;
		}
		
		string t;
		cin >> t;
		cout << v[t] << endl;
	}
	
	return 0;
}

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<<57;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int N, M;
ll cost[1000];
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) {
			if (conv.find(O[i]) == conv.end()) conv[O[i]] = conv.size() - 1;
			REP(j, K[i]) if (conv.find(Q[i][j]) == conv.end()) conv[Q[i][j]] = conv.size() - 1;
		}
		
		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 + 1000, INF);
		REP(i, N) chmin(cost[conv[S[i]]], P[i]);
		
		while (true) {
			bool update = false;
			REP(i, M) {
				ll sum = 0;
				FOR(j, 1, g[i].size()) {
					if (cost[g[i][j]] == INF) sum = INF;
					else sum += cost[g[i][j]];
				}
				update |= chmin(cost[g[i][0]], sum);
			}
			if (!update) break;
		}
		
		cout << cost[conv[T]] << endl;
	}
	return 0;
}

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

using namespace std;

int n, m;
struct Item {
    int id, price;
    string name;
    vector<int> edge;
    Item() {}
};
vector<Item> Graph;
int memo[101];

int Rec(int id)
{
    if (memo[id] != -1)
        return memo[id];

    int price = Graph[id].price, cmp = 0;

    for (int i = 0; i < Graph[id].edge.size(); ++i)
        cmp += Rec(Graph[id].edge[i]);

    return memo[id] = (cmp == 0) ? price : min(price, cmp);
}

int main()
{
    while (cin >> n, n) {
        string s, d;
        map<string, int> hash;

        for (int i = 0; i < n; ++i)
            memo[i] = -1;

        Graph.resize(n);
        for (int i = 0; i < n; ++i) {
            int p;

            cin >> s >> p;
            hash.insert(map<string, int>::value_type(s, i));
            Graph[i].id = i;
            Graph[i].price = p;
            Graph[i].name = s;
        }

        cin >> m;
        for (int i = 0; i < m; ++i) {
            int num;

            cin >> s >> num;
            for (int j = 0; j < num; ++j) {
                cin >> d;
                Graph[hash[s]].edge.push_back(hash[d]);
            }
        }

        cin >> s;
        cout << Rec(hash[s]) << endl;
    }

    return 0;
}