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<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

map<string, int> number;
vector<int> source[109];
int cost[109];
string str;

int solve(int x){
  int ans = 0;
  if(source[x].empty())return cost[x];
  for(int i = 0;i < source[x].size();i++){
    ans += solve(source[x][i]);
  }
  return min(cost[x], ans);
}

int main(){
  int n, p, m, k;
  while(1){
    number.clear();
    for(int i = 0;i < 109;i++)source[i].clear();
    cin >> n;
    if(n == 0)return 0;
    for(int i = 0;i < n;i++){
      cin >> str >> p;
      number[str] = i;
      cost[i] = p;
    }
    cin >> m;
    for(int i = 0;i < m;i++){
      cin >> str >> k;
      int num = number[str];
      for(int j = 0;j < k;j++){
	cin >> str;
	source[num].push_back(number[str]);
      }
    }
    cin >> str;
    cout << solve(number[str]) << endl;
  }
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 100
int N;
map<string, int> M;
map<string, int> id;

string contents[MAX_N];

vector<int> vec[MAX_N];

vector<int> ans_list;

int solve(int number){
	
	int ans = 0;
	for (int i = 0; i < vec[number].size(); i++){
		ans += solve(vec[number][i]);
	}

	if (ans == 0)
		return M[contents[number]];
	else
		return min(ans, M[contents[number]]);

}

int main(){

	while (true){
	
		cin >> N;
		if (N == 0)
			break;

		map<string, int> m;
		for (int i = 0; i < N; i++){
			vec[i].clear();
			string str;
			int cost;
			cin >> str >> cost;
			m[str] = cost;
			id[str] = i;
			contents[i] = str;
		}

		M = m;

		int num;
		cin >> num;
		for (int i = 0; i < num; i++){
			string s;
			int k;
			cin >> s >> k;
			int id_number = id[s];
			for (int j = 0; j < k; j++){
				string str1;
				cin >> str1;
				int number = id[str1];
				vec[id_number].push_back(number);
			}
		}

		string ans;
		cin >> ans;

		ans_list.push_back(solve(id[ans]));
	
	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int n;
map<string,int>M;
map<string,vector<string> >A;
map<string,bool>u;
int f(string s)
{
    if(u[s])return M[s];
    int now=M[s];
    if(A[s].size()>0)
    {
        int a=0;
        for(int i=0;i<A[s].size();i++)a+=f(A[s][i]);
        now=min(now,a);
    }
    u[s]=1;
    return M[s]=now;
}
main()
{
	while(cin>>n,n)
	{
		M.clear();
		A.clear();
		u.clear();
		for(int i=0;i<n;i++)
		{
			string s;int a;cin>>s>>a;M[s]=a;
		}
		int m;cin>>m;
		for(int i=0;i<m;i++)
		{
			string s;cin>>s;
			int k;cin>>k;
			for(;k--;)
			{
				string t;cin>>t;
				A[s].push_back(t);
			}
		}
		string s;cin>>s;
		cout<<f(s)<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cassert>
#include<map>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define N 100

const int INF = 1000100; 
int table[N];
int buycost[N];

int solve(int now,vector<int> *make){
  //if ( table[now] != -1)return table[now];
  if(  make[now].size()==0)return table[now]=buycost[now];
  

  int ret = buycost[now],tmp=0;  
  rep(i,make[now].size()){
    tmp+=solve(make[now][i],make);
  }
  
  ret= min(ret,tmp);
  return table[now]=ret; 
}

main(){
  int n;
  while(cin>>n && n){
    map<string,int> M;
    rep(i,n){
      string t;int a;
      cin>>t>>a;
      M[t]=i;
      buycost[i]=a;
      table[i]=-1;
    }
    
    vector<int> make[n];
    cin>>n;
    rep(i,n){
      string a;
      cin>>a;
      int now =M[a],m;
      cin>>m;
      rep(j,m){
	cin>>a;
	make[now].push_back(M[a]);
      }     
    }
    string tmp;
    cin>>tmp;
    int tar=M[tmp];
    cout << solve(tar,make) << endl;
    
  }
  return false;
}

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;
    }
    
}