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;
ll cost[200];
string S[100], O[100], Q[100][100], T;
ll 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();
		
		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]);
		
		while (true) {
			bool update = false;
			REP(i, M) {
				ll sum = 0;
				FOR(j, 1, g[i].size()) 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<map>
#include<string>
#include<vector>
using namespace std;
map<string,int>price;
map<string,vector<string> >rec;
int R(string o){
	int m=0,i;
	if(rec[o].size()){
		for(i=0;i<rec[o].size();i++)
			m+=R(rec[o][i]);
	}else
		m=price[o];
	if(price[o]&&price[o]<m)
		m=price[o];
	return m;
}
int main(){
	int n,m;
	string s,t;
	for(;cin>>n,n;){
		price.clear();
		rec.clear();
		for(;n--;){
			cin>>s;
			cin>>price[s];
		}
		cin>>n;
		for(;n--;){
			cin>>s>>m;
			int p=0;
			for(;m--;){
				cin>>t;
				p+=price[t];
			}
			if(p<price[s])
				price[s]=p;
		}
		cin>>s;
		cout<<price[s]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

struct recipe{
  int dist;
  vector<int> src;
};

int main(){
  int n;
  while(cin>>n, n){
    map<string,int> mp;
    vector<int> g(n);

    REP(i,n){
      string str;
      int p;
      cin >> str >> p;
      mp[str] = i;
      g[i] = p;
    }

    int m;
    cin >> m;
    vector<recipe> r(m);
    REP(i,m){
      string str;
      int k;
      cin >> str >> k;
      r[i].dist = mp[str];
      r[i].src = vector<int>(k);
      REP(j,k){
        cin >> str;
        r[i].src[j] = mp[str];
      }
    }

    bool flag = true;
    while(flag){
      flag = false;
      REP(i,m){
        int dist = r[i].dist;
        int sum = 0;
        REP(j,r[i].src.size()){
          sum += g[r[i].src[j]];
        }
        if(sum < g[dist]){
          flag = true;
          g[dist] = sum;
        }
      }
    }

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

C++: #include <iostream>
#include <unordered_map>
#include <vector>
#include <bitset>
#include <utility>
using namespace std;
#define MP make_pair
#define F first
#define S second
#define PB push_back
const int N=100 + 10;
int main(){
    int n,k,val[N],now,q,temp;
    bitset<N>b;
    bool f,c;
    string s;
    unordered_map<string,int> m;
    vector<vector<int> >graph[N];
    vector<int> emp;
    while(true){
        cin>>n;
        if(n==0)return 0;
        b.reset();
        for(int i=0;i<n;i++){
            cin>>s>>k;
            m.insert(MP(s,i));
            val[i]=k;
        }
        cin>>q;
        for(int i=0;i<q;i++){
            cin>>s>>k;
            now=m[s];
            b[now]=true;
            graph[now].PB(emp);
            while(k--){
                cin>>s;
                graph[now].back().PB(m[s]);
            }
        }
        while(q){
            for(int i=0;i<n;i++)if(!graph[i].empty()){
                f=true;
                for(int j=0;j<graph[i].size();j++)if(!graph[i][j].empty()){
                    f=false;
                    c=true;
                    temp=0;
                    for(int r=0;r<graph[i][j].size();r++){
                        if(b[graph[i][j][r]]){
                            c=false;
                            break;
                        }
                        else temp+=val[graph[i][j][r]];
                    }
                    if(c){
                        val[i]=min(val[i],temp);
                        graph[i][j].clear();
                        q--;
                    }
                }
                if(f){
                    graph[i].clear();
                    b[i]=false;
                }
            }
        }
        cin>>s;
        cout<<val[m[s]]<<endl;
    }
    
}

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

map<string,int> data;
map<string,map<string,int> >tree;

int cal(string str){
	int r=data[str];
	int sum=-1;
	map<string,int>::iterator itr=tree[str].begin();
	for(;itr!=tree[str].end();itr++){
		
		if(itr->second==1)sum+=cal(itr->first);
		
	}
	if(sum!=-1)return min(sum+1,r);
	else return r; 
}

int main(){
	int n;
	
	while(cin>>n && n){
		tree.clear();
		string str;
		int cost;
		for(int i=0;i<n;i++){
			cin>>str>>cost;
			data[str]=cost;
		}
		int m;
		cin>>m;
		for(int i=0;i<m;i++){
			string in;
			int num;
			cin>>in>>num;
			for(int j=0;j<num;j++){
				string tmp;
				cin>>tmp;
				tree[in][tmp]=1;
			}
		}
		
		string ma;
		cin>>ma;
		
		cout<<cal(ma)<<endl;
		
		
	}
	
}