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

int main(){
  int n,m,tmp2,sum;
  string tmp,make,fa;
  while(cin >> n,n){
    sum = 0;
    map <string,int> item;
    while(n--){
      cin >> tmp >> tmp2;
      item.insert(map<string,int>::value_type(tmp,tmp2));
    }
    cin >> m;
    while(m--){
      cin >> tmp >> tmp2;
      sum = 0;
      while(tmp2--){
	cin >> fa;
	if(item.find(fa)!=item.end()) sum += item[fa];
      }
      if(sum < item[tmp]) item[tmp] = sum;
    }
    cin >> make;
    cout << item[make] << endl;
  }
}

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

class Item{
public:
	string name;
	int cost;
	vector< Item* > recipe;
	
	Item(string _name, int _cost){
		name = _name;
		cost = _cost;
	}
	Item(){ name = ""; cost = 0; }
};

int Run(Item &item){
	int cost = 0;
	
	for(vector< Item* >::iterator it = item.recipe.begin(); it != item.recipe.end(); ++it){
		cost += Run( (**it) );
	}
	
	if(!item.recipe.empty()){
		return min(item.cost, cost);
	}
	return item.cost;
}

int main(){
	int n, m, p, k;
	string i, o, q, item;
	map< string, Item > mp;
	
	while(cin >> n, n){
		mp.clear();
		for(int j = 0; j < n; ++j){
			cin >> i >> p;
			mp.insert(make_pair< string, Item >(i, Item(i, p)));
		}
		cin >> m;
		
		for(int i = 0; i < m; ++i){
			cin >> o >> k;
			for(int j = 0; j < k; ++j){
				cin >> q;
				mp[o].recipe.push_back(&mp[q]);
			}
		}
		cin >> item;
		
		cout << Run(mp[item]) << endl;
	}
	
	return 0;
}

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

int main(){
	int n,m,a,b;
	string s,ts;
	while(cin >> n,n){
		map<string,int> ma;
		for(int i = 0;i < n;++i){
			cin >> s >> a;
			ma[s] = a;
		}
		cin >> m;
		for(int i = 0;i < m;++i){
			b = 0;
			cin >> s >> a;
			for(int j = 0;j < a;++j){
				cin >> ts;
				b += ma[ts];
			}
			if(b < ma[s]) ma[s] = b;
		}
		cin >> s;
		cout << ma[s] << endl;
	}
	return 0;
}

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

const int N = 100;

vector<int> recipe[N];//store recipe
int price[N];           //store price


int solve(int now){
  int ret = price[now];
  int tmp = 0;
  if (recipe[now].size() == 0)return ret;
  for(int i=0;i<recipe[now].size();i++){
    tmp+=solve(recipe[now][i]);
  }
  return min(ret,tmp);
}


main(){
  int n;
  while(cin>>n && n){
    int p;
    map<string,int> M;
    string tmp;
    
    //get buy information
    for(int i=0;i<n;i++){
      cin>>tmp>>p;
      M[tmp]=i;
      price[i]=p;
      recipe[i].clear();
    }
    
    //get recipe
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
      int num,me;
      cin>>tmp>>num;
      me=M[tmp];
      for(int j=0;j<num;j++){
	cin>>tmp;
	recipe[me].push_back(M[tmp]);
      }
    }


    cin>>tmp;
    cout << solve(M[tmp]) <<endl;
  }
}

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

int main ()
{
    int n;
    while (cin >> n, n) {
        map<string,int> atm;
        for (int i = 0; i < n; i++) {
            int b; string a;
            cin >> a >> b;
            atm[a] = b;
        }
        int m;  cin >> m;
        map<string, queue<string> > rcp;
        for (int i = 0; i < m; i++) {
            string str; int t;
            cin >> str >> t;
            queue<string> q;
            int r_cost = 0;
            for (int j = 0; j < t; j++) {
                string s; cin >> s;
                r_cost += atm[s];
                q.push(s);
            }
            if (atm.find(str) != atm.end()) atm[str] = min(atm[str], r_cost);
            else atm[str] = r_cost;
            rcp[str] = q;
        }
        string pro; cin >> pro;
        if (rcp.find(pro) == rcp.end()) cout << atm[pro] << endl;
        else {
            int res = 0;
            while (!rcp[pro].empty()) {
                string s = rcp[pro].front(); rcp[pro].pop();
                res += atm[s];
            }
            cout << (min(atm[pro], res)) << endl;
        }
    }

    return 0;
}

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

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

int dfs(string& name,map<string,int>& state){
    state[name]=1;
    int res=table[name];
    if(recipe[name].empty()) return res;

    int tmp=0;
    for(auto &next:recipe[name]){
        if(state[next]==1){
            state[name]=2;
            return opt[name]=res;
        }
        if(state[next]==2) tmp+=opt[next];
        else tmp+=dfs(next,state);
    }
    res=min(res,tmp);

    state[name]=2;
    return opt[name]=res;
}

void solve(){
    int n;
    while(true){
        cin >> n;
        if(!n) break;

        table.clear();
        recipe.clear();
        opt.clear();

        rep(i,0,n){
            string name;
            int cost;
            cin >> name >> cost;
            table[name]=cost;
            opt[name]=inf;
        }

        int m;
        cin >> m;
        rep(i,0,m){
            string name1;
            int k;
            cin >> name1 >> k;
            rep(j,0,k){
                string name2;
                cin >> name2;
                recipe[name1].push_back(name2);
            }
        }

        string str;
        cin >> str;
        map<string,int> state;
        cout << dfs(str,state) << endl;
    }
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}

C++: #include <map>
#include <iostream>
#include <vector>
using namespace std;
map<string,int> item;
map<string,vector<string> > rec;

int saiki(string str){
	int ret = 0;
	if(rec[str].size()){
		for(int i=0;i<rec[str].size();i++){
			ret += saiki(rec[str][i]);
		}
	}else{
		ret+=item[str];
	}
	return ret;
}

int main(){
	int n,m,t;
	string s,rs;
	while(cin >> n && n){
		item.clear();
		rec.clear();
		for(int i=0;i<n;i++){
			cin >>	s;
			cin >> item[s];
		}
		cin >> m;
		for(int i=0;i<m;i++){
			cin >> rs >> t;
			for(int j=0;j<t;j++){
				cin >> s;
				rec[rs].push_back(s);
			}
		}
		cin >> s;
		cout << saiki(s) << endl;
	}
}