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 <cmath>
#include <map>

#define INF (1 << 8)

using namespace std;

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

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

int main()
{
    int N, R; 
    while(cin >> N && N) {
        table.clear();
        for(int i = 0; i < N; i++) {
            int v; string name;
            cin >> name >> v;
            table[name] = v;
        }
        
        cin >> R;
        recipe.clear();
        for(int i = 0; i < R; i++) {
            int n; string name, tmp; vector<string> child;
            cin >> name >> n;
            for(int j = 0; j < n; j++) {
                cin >> tmp;
                child.push_back(tmp);
            }
            recipe[name] = child;
        }

        string query;
        cin >> query;
        cout << rec(query) << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;

int main(){
  int n, m;
  map< string, int > peace;
  
  
  while(cin >> n, n){
    for(int i = 0; i < n; i++){
      string s;
      int p;
      cin >> s >> p;
      peace.insert( make_pair( s, p));
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      string a;
      int c, cost = 0;
      cin >> a >> c;
      while(c--){
        string b;
        cin >> b;
        cost += peace[b];
      }
      peace[a] = min( peace[a], cost);
    }
    string query;
    cin >> query;
    cout << peace[query] << endl;
    peace.clear();
  }
}

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

using namespace std;

typedef long long ll;

const int MAX = 100;
int n;
string name;

int main(){

  while(cin >> n && n){
    map<string,ll> M;
    for(int i = 0; i < n; i++){
      int price;
      cin >> name >> price;
      M[name] = price;
    }
    int rev;
    cin >> rev;
    for(int i = 0; i < rev; i++){
      int num;
      cin >> name >> num;
      ll sum = 0;
      for(int j = 0; j < num; j++){
	string str;
	cin >> str;
	sum += M[str];
      }
      M[name] = min(M[name],sum);
    }
    cin >> name;
    cout << M[name] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int main(){
      int n;
      while(cin>>n&&n){
            string s,t[100];
            int p,m;
            bool update=true;
            map<string,int>M;
            string o[100];vector<string> pp[100];
            REP(i,n){
                  cin>>t[i]>>p;
                  M[t[i]]=p;
            }
            cin>>m;
            REP(i,m){
                  cin>>o[i]>>p;
                  REP(j,p){
                        cin>>s;pp[i].PB(s);
                  }
            }
            while(update){
                  update=false;
                  REP(i,m){
                        int cost=0;
                        REP(j,pp[i].size()){
                              cost+=M[pp[i][j]];
                        }
                        if(M[o[i]]>cost){
                              M[o[i]]=cost;
                              update=true;
                        }
                  }
            }
            cin>>s;
            cout<<M[s]<<endl;
      }
}

C++: #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int n;
int price[100];
vi recipe[100];
int memo[100];
int search(int id)
{
	if(memo[id] >= 0)
		return memo[id];
	if(memo[id] == -2)
		return price[id];
	if(recipe[id].empty())
		return memo[id] = price[id];
	
	memo[id] = -2;
	int sum = 0;
	rep(i, recipe[id].size()){
		sum += search(recipe[id][i]);
	}
	return memo[id] = min(price[id], sum);
}

int main()
{
	int n;
	while(scanf("%d", &n), n){
		fill(memo, memo+n, -1);
		
		map<string, int> dict;
		rep(i, n){
			char tmp[128];
			scanf("%s%d", tmp, &price[i]);
			dict[tmp] = i;
		}
		
		int m;
		scanf("%d", &m);
		rep(i, m){
			char tmp[128];
			scanf("%s", tmp);
			int id = dict[tmp];
			int k;
			scanf("%d", &k);
			rep(j, k){
				scanf("%s", tmp);
				recipe[id].push_back(dict[tmp]);
			}
		}
		
		char tmp[128];
		scanf("%s", tmp);
		printf("%d\n", search(dict[tmp]));
	}
	return 0;
}

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

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    map<string, int> price;
    for (int i = 0; i < n; ++i) {
      string s;
      int p;
      cin >> s >> p;
      price[s] = p;
    }

    int m;
    cin >> m;
    map<string, vector<string> > alchemy;
    for (int i = 0; i < m; ++i) {
      string s;
      cin >> s;
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
	string q;
	cin >> q;
	alchemy[s].push_back(q);
      }
    }

    bool update = true;
    while (update) {
      update = false;
      for (map<string, vector<string> >::iterator it = alchemy.begin(); it != alchemy.end(); ++it) {
	int sum = 0;
	for (int i = 0; i < it->second.size(); ++i)
	  sum += price[it->second[i]];

	if (price[it->first] > sum) {
	  price[it->first] = sum;
	  update = true;
	  break;
	}
      }
    }

    string target;
    cin >> target;
    cout << price[target] << endl;
  }
  return 0;
}