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 <stdio.h>
#include <vector>
#include<string>
using namespace std;
int ha(string);
void saga(void);

struct L{
	string name;
	int cost;
};
struct  R{
	string name;
	string zairyo[101];
	int size;
};
int q,m,n;
L list[101];
R resipi[101];

int main()
{	
	int i,j,k,b=0,s,a;
	string S;
	cin>> n;
	while(n!=0){
	q=n;
	for(i=0;i<n;i++){
		cin  >> list[i].name>> list[i].cost;
	}


	cin>>m;
	for(i=0;i<m;i++){
		cin>>resipi[i].name>>b;
		resipi[i].size=b;
		for(j=0;j<b;j++){
		cin>>resipi[i].zairyo[j];
		}
		
	}
	cin>>S;

	
	/*for(i=0;i<n;i++){
		cout <<list[i].name<< list[i].cost<<endl;
	}
	for(i=0;i<m;i++){
		cout<<resipi[i].name;
		for(j=0;j<resipi[i].size;j++){
			cout<<resipi[i].zairyo[j];
		}
		cout<<endl;
	}
	*/
	cout<<ha(S)<<endl;
	cin>>n;
	}
	return 0;

}
int ha(string S)
{
	int  i,j,f=0,sum=0;
	for(i=0;i<m;i++){
		//cout<<S<<endl<<resipi[i].name;	
	if(S==resipi[i].name){
		f=1;
		break;
	}
	}
	if(f==1){
		for(j=0;j<resipi[i].size;j++){
			sum=sum+ha(resipi[i].zairyo[j]);
			}
		//printf("%d\n",sum);
		for(i=0;i<n;i++){
		if(S==list[i].name){
		if(sum<list[i].cost){
		return sum;
		}
		else return list[i].cost;
		}
	}
		
		}

	for(i=0;i<n;i++){
		if(S==list[i].name){
		return list[i].cost;
		}
	
	}

	
}

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

#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long

int calc(string name,map<string,int> data,map<string,vector<string> > recipe)
{
    if (recipe.find(name) != recipe.end())
    {
	int tmp=0;
	rep(i,recipe[name].size())
	{
	    tmp+=calc(recipe[name][i],data,recipe);
	}
	return min(tmp,data[name]);
    } else {
	return data[name];
    }
}

int main()
{
    while(1)
    {
	string name,tmp;
	int price,n,m;
	map<string,int> d;
	map<string,vector<string> > recipe;
	cin >> n;
	if (n==0)break;
	rep(i,n)
	{
	    cin >> name >> price;
	    d[name]=price;
	}
	cin >> m;
	rep(i,m)
	{
	    vector<string> v;
	    cin >> name >> n;
	    rep(j,n)
	    {
		cin >> tmp;
		v.push_back(tmp);
	    }
	    recipe[name] = v;
	}
	cin >> name;
	cout << calc(name,d,recipe) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
	int n,m;
	while(cin>>n&&n){
		map<string,long long> data;
		string s;
		int a;
		for(int i=0;i<n;i++){
			cin>>s>>a;
			data[s]=a;
		}
		cin>>m;
		string wrought[101][101];
		int k,sum;
		for(int i=0;i<m;i++){
			cin>>wrought[i][0];
			cin>>k;
			for(int j=0;j<k;j++)cin>>wrought[i][j+1];
		}
		bool update=true;
		while(update){
			update=false;
			for(int i=0;i<m;i++){
				sum=0;
				for(int j=0;j<k;j++)sum+=data[wrought[i][j+1]];
				if(data[wrought[i][0]]>sum){
					data[wrought[i][0]]=sum;
					update=true;
				}
			}
		}
		string ans;
		cin>>ans;
		cout<<data[ans]<<endl;
	}
}

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

using namespace std;

int n, m;
struct Item {
    int id, price;
    string name;
    vector<int> edge;
    Item() {}
};
vector<Item> Graph;
int memo[101];

int Rec(int id)
{
    if (memo[id] != -1)
        return memo[id];

    int price = Graph[id].price, cmp = 0;

    for (int i = 0; i < Graph[id].edge.size(); ++i)
        cmp += Rec(Graph[id].edge[i]);

    return memo[id] = (cmp == 0) ? price : min(price, cmp);
}

int main()
{
    while (cin >> n, n) {
        string s, d;
        map<string, int> hash;

        Graph.resize(n);
        for (int i = 0; i < n; ++i) {
            memo[i] = -1;
            Graph[i].edge.clear();
        }

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

            cin >> s >> p;
            hash.insert(map<string, int>::value_type(s, i));
            Graph[i].id = i;
            Graph[i].price = p;
            Graph[i].name = s;
        }

        cin >> m;
        for (int i = 0; i < m; ++i) {
            int num;

            cin >> s >> num;
            for (int j = 0; j < num; ++j) {
                cin >> d;
                Graph[hash[s]].edge.push_back(hash[d]);
            }
        }

        cin >> s;
        cout << Rec(hash[s]) << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define rep(I,N) for(int I = 0; I < (int)(N); I++)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int solve(map<string, int> list, map<string, vector<string> > lesp, string make){
	vector<string> item = lesp[make];
	ll mini = INF;
	ll cost = 0;
	rep(i, item.size()){
		if(!lesp[item[i]].empty()){
			cout << "item" << endl;
			cost += solve(list, lesp, item[i]);
		}else{
			cost += list[item[i]];
		}
	}
	mini = min(mini, cost);
	return mini;
}

int main(void){
	int n, m;
	while(cin >> n, n){
		map<string, int> LIST;
		string MAKE;
		rep(i, n){
			string s;
			int cost;
			cin >> s >> cost;
			LIST[s] = cost;
		}
		cin >> m;
		map<string, vector<string> > LESP;
		rep(i, m){
			string s;
			cin >> s;
			int num;
			cin >> num;
			vector<string> v;
			rep(j, num){
				string L;
				cin >> L;
				v.push_back(L);
			}
			LESP[s] = v;
		}
		cin >> MAKE;
		if(m == 0){
			cout << LIST[MAKE] << endl;
			continue;
		}
		int ans = solve(LIST, LESP, MAKE);
		FIN(ans);

	}


	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
using namespace std;
int n;
map<string,int> t;
int c[111];
vector<int> v[111];

int solve(int m){
  if(v[m].empty()) return c[m];
  int sum = 0;
  for(int i = 0; i < (int)v[m].size(); i++){
    sum += solve(v[m][i]);
  }
  return min(sum,c[m]);
}

int main(void){
  while(cin >> n && n){
    t.clear();
    for(int i = 0; i < 111; i++) v[i].clear();
    while(n--){
      string s;
      int a;
      cin >> s >> a;
      t[s] = n;
      c[n] = a;
    }
    cin >> n;
    while(n--){
      string s;
      int a;
      cin >> s >> a;
      int id = t[s];
      for(int i = 0; i < a; i++){
	cin >> s;
	v[id].push_back(t[s]);
      }
    }
    string s;
    cin >> s;
    cout << solve(t[s]) << endl;
  }
}