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<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];
  
  table[now]=0;
  int ret = buycost[now],tmp=0;  
  rep(i,make[now].size()){
    tmp+=solve(make[now][i],make);
  }
  
  ret= min(ret,tmp);
  return table[now]=tmp; 
}

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<bits/stdc++.h>
using namespace std;
int main(){
  while(1){
    int n;
    string buhin[100];
    int bup[100];
    int m;
    int o;
    string buren[100];
    
    cin>>n;
    if(n==0) break;
    for(int i=0;i<n;i++){
      cin>>buhin[i]>>bup[i];
    }
    cin>>m;
    for(int i=0;i<m;i++){
      string rnk;
      int rnkp;
      int hikaku=0;
      cin>>rnk>>o;
      for(int j=0;j<n;j++){
	if(rnk==buhin[j]){
	  rnkp=bup[j];
	}
      }
      for(int j=0;j<o;j++){
	cin>>buren[j];
	for(int k=0;k<n;k++){
	  if(buren[j]==buhin[k]){
	    hikaku+=bup[k];
	  }
	}	
      }
      
      if(hikaku<rnkp){
	for(int j=0;j<n;j++){
	  if(rnk==buhin[j]){
	    bup[j]=hikaku;
	  }
	}
      }
    }
    string c;
    cin>>c;
    for(int i=0;i<n;i++){
      if(c==buhin[i]){
	cout<<bup[i]<<endl;
      }
    }
  }
}

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

using namespace std;

vector<int> list[1000];
int item[1000];

int saiki(int num){
	if(list[num].empty()==1){
		return item[num];
	}
	
	int cont=0;
	for(int i=0;i<list[num].size();i++){
		cont+=saiki(list[num][i]);
	}
	
	return min(cont,item[num]);
}

int main(){
	
	map<string,int> name;
	
	while(1){
	
	int n,m;
	cin>>n;
	if(n==0){
		break;
	}
	for(int i=0;i<n;i++){
		string a;int b;
		cin>>a>>b;
		name[a]=i;
		item[i]=b;
	}
	
	for(int i=0;i<n;i++){
		list[i].clear();
	}
	
	cin>>m;
	for(int i=0;i<m;i++){
		string a;int b;
		cin>>a>>b;
		for(int j=0;j<b;j++){
			string c;
			cin>>c;
			list[name[a]].push_back(name[c]);
		}
	}
	string a;
	cin>>a;
	
	printf("%d\n",saiki(name[a]));
	}
	
	
}

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

using namespace std;
typedef unsigned long long ULL;

struct Dict {
    map<string,int> dict;
    int id;
    Dict() : id(0) {}

    int get(const string &name) {
        if(dict.count(name)) return dict[name];
        return dict[name] = id++;
    }
};

ULL dfs(int pos, vector<ULL> &memo, vector<int> &visiting, const vector<vector<int> > &materials) {
    const vector<int> &list = materials[pos];
    if(visiting[pos]) return memo[pos];
    if(list.size() == 0) return memo[pos];

    visiting[pos] = 1;
    ULL acc = 0;
    for(vector<int>::const_iterator it = list.begin(); it != list.end(); ++it) {
        acc += dfs(*it, memo, visiting, materials);
    }
    visiting[pos] = 0;
    return memo[pos] = min(acc, memo[pos]);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(true) {
        int N;
        cin >> N;
        if(!N) break;

        Dict dict;
        vector<ULL> price(N);
        vector<vector<int> > materials(N);
        for(int i = 0; i < N; ++i) {
            string name;
            int p;
            cin >> name >> p;
            price[dict.get(name)] = p;
        }
        int M;
        cin >> M;
        for(int i = 0; i < M; ++i) {
            string to;
            cin >> to;
            const int to_id = dict.get(to);
            int cnt;
            cin >> cnt;
            while(cnt--) {
                string material;
                cin >> material;
                materials[to_id].push_back(dict.get(material));
            }
        }
        string target;
        cin >> target;
        vector<ULL> memo(price);
        vector<int> visiting(N, 0);
        cout << dfs(dict.get(target), memo, visiting, materials) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main()
{
	string items[101];
	int p[101];
	int n,m,i,j,k,l;
	
	for(;;)
	{
		cin >> n ;
		if(n==0)
			break;
		for(i=0;i<n;i++)
		{
			cin >> items[i] >> p[i] ;
		}
		
		cin >> m ;
		for(i=0;i<m;i++)
		{
			int s_p=0;
			string item,s_item;
			int nm;
			cin >> item >> nm ;
			for(k=0;k<n;k++)
			{
				if(items[k]==item)
					break;
			}
			for(j=0;j<nm;j++)
			{
				cin >> s_item;
				for(l=0;l<n;l++)
				{
					if(s_item==items[l])
					{
						s_p+=p[l];
						break;
					}
				}
			}
			p[k]=min(p[k],s_p);
		}
		string last;
		cin >> last;
		for(i=0;i<n;i++)
		{
			if(items[i]==last)
			{
				cout << p[i] << endl;
				break;
			}
		}
	}
}

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

struct material
{
    int price;
    vector<string> need;
};

void solve(int);

int main()
{
    int n;

    while (cin >> n, n){
        solve(n);
    }

    return 0;
}

void solve(int n)
{
    int m;
    map<string, material> mmap;

    for (int i = 0; i < n; i++){
        string s;
        int p;

        cin >> s >> p;
        mmap[s] = { p, vector<string>() };
    }

    cin >> m;
    for (int i = 0; i < m; i++){
        string o;
        int k;
        int sum = 0;

        cin >> o >> k;
        while (k-- > 0){
            string q;

            cin >> q;
            mmap[o].need.push_back(q);
            sum += mmap[q].price;
        }
        mmap[o].price = min(mmap[o].price, sum);
    }

    string s;
    cin >> s;
    printf("%d\n", mmap[s].price);
}