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 <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
using namespace::std;
int n;

struct item{
    string name;
    int cost;
    vector<string> used;
    int size;
} ;

item list[100];
int memo[100];

int getCost(int v)
{
    if(memo[v]!=-1)return memo[v];
    int i, j;
    int ans;
    
    if (list[v].size == 0){
        return (list[v].cost);
    }
    else {
        ans = 0;
        for (i = 0; i < list[v].size; i++){
            for (j = 0; j < n; j++){
                if (list[v].used[i]== list[j].name){
                    ans += getCost(j);
                }
            }
        }
        list[v].cost = min(list[v].cost,ans);
    }
    return (list[v].cost);
}

int main(void)
{
    int i, j, k;
    int m, kk;
    string key, temp;
    int look, sum;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        for(i=0;i<n;i++)memo[i]=-1;
        for (i = 0; i < n; i++){
            cin >> list[i].name >> list[i].cost;
            list[i].size = 0;
        }
        
        cin >> m;
        for (i = 0; i < m; i++){
            cin >> key;
            for (j = 0; j < n; j++){
                if (key==list[j].name){
                    look = j;
                }
            }
            cin >> kk;
            list[look].size = kk;
            for (j = 0; j < kk; j++){
                cin >> temp;
                for (k = 0; k < n; k++){
                    if (temp==list[k].name){
                        list[look].used[j]=temp;
                    }
                }
            }
        }
        cin >> key;
        for (j = 0; j < n; j++){
            if (key==list[j].name){
                look = j;
            }
        }
        
        cout << getCost(look);
        
    }
    
    return (0);
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
struct item
{
	string name;
	int cost;
	bool havelist;
	vector<string> le;
};
item lis[100];
int memo[100];
int mysearch(string s1)
{
	for(int i=0;i<n;i++)
	{
		if(lis[i].name==s1)return i;
	}
}
int getcost(int v)
{
	if(memo[v]!=-1)return memo[v];
	else
	{
		if(!(lis[v].havelist))return lis[v].cost;
		else
		{
			int ans=0;
			for(int i=0;i<lis[v].le.size();i++)
			{
				ans+=getcost(mysearch(lis[v].le[i]));
			}
			lis[v].cost=min(lis[v].cost,ans);
			memo[v]=lis[v].cost;
			return memo[v];
		}
	}
}
int main()
{
	while(1)
	{
		cin >> n;
		if(n==0)break;
		fill(memo,memo+100,-1);
		string s;
		int c;
		for(int i=0;i<n;i++)
		{
			cin >> s >> c;
			lis[i].name=s;
			lis[i].cost=c;
			lis[i].havelist=false;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			string st;
			int t,k;
			cin >> st;
			cin >> t;
			k = mysearch(st);
			for(int j=0;j<t;j++)
			{
				string str;
				cin >> str;
				lis[k].le.pb(str);
			}
			lis[k].havelist=true;
		}
		string stri;
		cin >> stri;
		cout << getcost(mysearch(stri)) << endl;
	}
	return 0;
}

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

int main() {
	int a;
	while (scanf("%d", &a), a) {
		map<string, int>b;
		for (int c = 0; c < a; c++) {
			string d; int e;
			cin >> d;
			scanf("%d", &e);
			b[d] = e;
		}
		int f;
		scanf("%d", &f);
		map<string, vector<string>>h;
		for (int g = 0; g < f; g++) {
			string i; int j;
			cin >> i;
			scanf("%d", &j);
			vector<string>k(j);
			for (int l = 0; l < j; l++)cin >> k[l];
			h[i] = k;
		}
		for (int x = 0; x < a; x++) {
			bool y = false;
			for (auto z = h.begin(); z != h.end(); z++) {
				int q = 0;
				for (int p = 0; p < z->second.size(); p++) {
					q += b[z->second[p]];
				}
				if (q < b[z->first]) {
					y = true;
					b[z->first] = q;
				}
			}
			if (!y)break;
		}
		string s;
		cin >> s;
		cout << b[s] << endl;
	}
}

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

using namespace std;

map<string,int> dp;
map<string,int> buy;
map<string, vector<string> > reci;

const int INF = 1000000;

#define rep(i,a) for(int i=0; i<(a); i++)

int solve(string to){
  int rtn;
  if(dp[to] == INF){
    if(reci[to].size() != 0){
      int ari = 0;
      rep(i,reci[to].size()){
	ari += solve(reci[to][i]);
      }
      rtn = min(buy[to] , ari);
    } else {
      rtn = buy[to];
    }
    dp[to] = rtn;
  } else {
    rtn = dp[to];
  }
  return rtn;
}


int main(){
  int n;
  while(cin >> n, n){
    dp.clear();
    buy.clear();
    reci.clear();
    rep(i,n){
      string str; int p;
      cin >> str >> p;
      buy[str] = p;
      dp[str] = INF;
    }
    int r; cin >> r;
    rep(i,r){
      string str; int m;
      cin >> str >> m;
      rep(j,m){
	string ing;
	cin >> ing;
	reci[str].push_back(ing);
      }
    }

    string to; cin >> to;
    cout << solve(to) << endl;
  }
  return 0;
}

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

struct ITEM {
	string name;
	int cost;
	int recipeNum;
	int recipe[100];
};

int itemNum;
ITEM item[100];

int renkin(int itemId);
int searchId(string name);

int main(){
	int i,j,n;
	string name;

	//cout<<"test\n";

	while(cin>>itemNum,itemNum){
		//item
		for(i=0;i<itemNum;i++) cin>>item[i].name>>item[i].cost;

		//recipe
		cin>>n;
		while(n--){
			cin>>name;i=searchId(name);
			cin>>item[i].recipeNum;
			for(j=0;j<item[i].recipeNum;j++){
				cin>>name;
				item[i].recipe[j]=searchId(name);
			}
		}

		cin>>name;
		cout<<renkin(searchId(name))<<endl;
	}

	return 0;
}

int renkin(int itemId){
	int result=0;
	int i;
	
	for(i=0;i<item[itemId].recipeNum;i++) result+=renkin(item[itemId].recipe[i]);
	if(i==0 || item[itemId].cost<result) result=item[itemId].cost;

	return result;
}

int searchId(string name){
	int i=0;while(name!=item[i].name) i++;
	return i;
}