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<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))


int main(){
	map<string,int> m;
	map<string,vector<string> > l;
	vector<string> str;
	int n,v,t;
	string s,u;
	for(;;){
		cin >> n;
		if(!n)break;
		m.clear();
		l.clear();
		while(n--){
			cin >> s >> v;
			m[s]=v;
		}
		cin >> n;
		while(n--){
			cin >> s >> t;
			str.push_back(s);
			while(t--){
				cin >> u;
				l[s].push_back(u);
			}
		}
		t=1;
		while(t){
			t=0;
			rep(i,str.size()){
				v=0;
				rep(j,l[str[i]].size())v += m[l[str[i]][j]];
				if(m[str[i]]>v){
					m[str[i]]=v;
					t=1;
				}
			}
		}
		cin >> s;
		cout << m[s] << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
using namespace std;
pair<string, int>p[101];
pair<string, vector<string>>chan[101];
int search(string);	
int n;
int m,si;
int main()
{
	
	while (true){
		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);
		cout << ans << endl;
	}
}
int search(string s){
	int num = 0;
	bool flag = true;
	for (int i = 0; i < m; i++){
		if (chan[i].first == s){
			flag = false;
			for (int i2 = 0; i2 < chan[i].second.size(); i2++){
				num += search(chan[i].second[i2]);
			}
		}
	}
	if (flag){
		for (int i = 0; i < n; i++){
			if (p[i].first == s){
				num += p[i].second;
			}
		}
	}
	return num;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

const int INF = 1 << 30;
int rec(const int id, map<int, int>& goods, const vector<vint>& table)
{
	int sum = INF;

	if(!table[id].empty())
	{
		sum = 0;
		rep(i, table[id].size())
			sum += rec(table[id][i], goods, table);
	}

	return min(goods[id], sum);
}

int main()
{
	int n;
	while(cin >> n && n)
	{
		map<string, int> namecon;
		map<int, int>    goods;
		vector<vint> d(n, vint(n));

		rep(i, n)
		{
			string name;
			int cost;

			cin >> name >> cost;

			namecon.insert(map<string, int>::value_type(name, i));
			goods.insert(map<int, int>::value_type(i, cost));
		}

		int m;
		cin >> m;
		vector<vint> table(n);

		rep(i, m)
		{
			int num;
			string name;
			cin >> name >> num;

			rep(j, num)
			{
				string mat;
				cin >> mat;
				table[namecon[name]].push_back(namecon[mat]);
			}
		}

		string item;
		cin >> item;

		cout << rec(namecon[item], goods, table) << 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);
			}
		}
		
		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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m;
map<string, ll> mp;

int main() {
  while(true) {
    cin >> n;
    if (n == 0) break;
    rep(i, n) {
      string s; ll t;
      cin >> s >> t;
      mp[s] = t;
    }

    cin >> m;
    rep(i, m) {
      int ti;
      string s;
      ll res=0;
      cin >> s >> ti;
      rep(j, ti) {
        string st;
        cin >> st;
        res += mp[st];
      }
      mp[s] = min(res, mp[s]);
    }

    string ress;
    cin >> ress;
    cout << mp[ress] << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

struct recipe{
	string n;
	vector<string> vs;
};

int main(){
	int n, m;
	int cost;
	string name;
	while(cin >> n){
		if(n == 0) break;
		map<string, int> costs;
		for(int i = 0; i < n; i++){
			cin >> name >> cost;
			costs[name] = cost;
		}
		cin >> m;
		vector<recipe> vr;
		for(int i = 0; i < m; i++){
			recipe r;
			int j;
			cin >> r.n;
			cin >> j;
			for(int k = 0; k < j; k++){
				cin >> name;
				r.vs.push_back(name);
			}
			vr.push_back(r);
		}
		for(int i = 0; i < n; i++){
			for(vector<recipe>::iterator iter = vr.begin(); 
				iter != vr.end(); iter++)
			{
				int cost = 0;
				for(vector<string>::iterator it = (*iter).vs.begin();
					it != (*iter).vs.end(); it++)
				{
					cost += costs[*it];
				}
				if(cost < costs[(*iter).n])
					costs[(*iter).n] = cost;
			}
		}
		cin >> name;
		cout << costs[name] << endl;
	}
	return 0;
}