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<cstdio>
#include<map>
#include<vector>
#include<string>

using namespace std;

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

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

int main(){
	while(1){
		map<string,int> name;
		int n;
		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<100;i++){
			list[i].clear();
		}
		
		int m;
		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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[],int b[],int m,int n,string s[],vector<string> t[],string u){
  int i,j;
  int c[100],g=-1;
  int sum;
  for(i=0;i<m;i++){
    if(b[i]&&t[i][0]==u)
      return -1;
  }
  for(i=0;i<n;i++){
    if(s[i]==u)
      break;
  }
  if(i!=n)
    g=a[i];
  for(i=0;i<m;i++){
    if(t[i][0]==u){
      memcpy(c,b,sizeof(c));
      c[i]=1;
      sum=0;
      for(j=1;j<(int)t[i].size();j++){
	int tmp;
	if((tmp=f(a,c,m,n,s,t,t[i][j]))==-1)
	  return g;
	else
	  sum+=tmp;
      }
      if(g==-1||g>sum)
	g=sum;
    }
  }
  return g;
}
int main(){
  int i,j,k;
  int a[100],b[100],m,n;
  string s[100],u;
  vector<string> t[100];
  while(cin>>n&&n){
    for(i=0;i<n;i++)
      cin>>s[i]>>a[i];
    cin>>m;
    for(i=0;i<m;i++){
      cin>>u>>k;
      t[i].clear();
      t[i].push_back(u);
      for(j=0;j<k;j++){
	cin>>u;
	t[i].push_back(u);
      }
    }
    cin>>u;
    memset(b,0,sizeof(b));
    cout<<f(a,b,m,n,s,t,u)<<endl;
  }
  return 0;
}

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

#define mp make_pair
#define rep(i,n) for(int i = 0 ; i < n ; i++)

using namespace std;

class pred {
	string str;
public:
	pred(const string& _str) : str(_str) {}
	bool operator()( const pair< string , int > pr ) { return pr.first == str; }
};

int main(){
	
	int n;
	string s_ans;
	
	cin >> n;
	while(n)
	{
		vector< pair<string , int> > item;
		vector< pair<string , int> > :: iterator ite;
		
		rep(i,n) 
		{
			string a;
			int	b;
			cin >> a >> b;
			item.push_back( mp( a , b ) );
		}
		
		cin>>n;
		
		rep(i,n)
		{
			string s;
			int m , goukei=0;
			cin >> s >>m;
			rep(i,m)
			{
				string dummy;
				cin >> dummy;
				ite = find_if( item.begin() , item.end() , pred( dummy ) );
				goukei += (*ite).second;
			}
			ite = find_if( item.begin() , item.end() , pred( s ) );
			if( (*ite).second > goukei )
			{
				ite = remove_if( item.begin() , item.end(), pred( s ) );
				item.erase( ite );
				item.push_back( mp( s , goukei )  );
			}
		}
		
		cin>>s_ans;
		ite = find_if( item.begin() , item.end() , pred( s_ans ) );
		cout << (*ite).second << endl;
		cin>>n;
	}
	return 0;
}

C++: // 2014/10/27 Tazoe

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

map<string, int> Item;
map<string, vector<string> > Recipe;

int getCost(string name)
{
	if(Recipe.find(name)==Recipe.end())	// レシピがなかったら
		return Item[name];

	int sum = 0;
	for(int i=0; i<Recipe[name].size(); i++){
		sum += getCost(Recipe[name][i]);
	}

	return min(sum, Item[name]);
}

int main()
{
	while(true){
		int n;
		cin >> n;

		if(n==0)
			break;

		Item.clear();
		for(int i=0; i<n; i++){
			string s;
			int p;
			cin >> s >> p;

			Item[s] = p;
		}

		int m;
		cin >> m;

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

			for(int j=0; j<k; j++){
				string q;
				cin >> q;

				Recipe[o].push_back(q);
			}
		}

		string name;
		cin >> name;

		cout << getCost(name) << 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 usedNum;
int used[100];

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

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

	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;usedNum=0;
		cout<<renkin(searchId(name))<<endl;
	}

	return 0;
}

int renkin(int itemId){
	int work,result=0;
	int i,j;
	
	used[usedNum++]=itemId;
	for(i=0;i<item[itemId].recipeNum;i++){
		j=0;while(j<usedNum && used[j]!=item[itemId].recipe[i]) j++;
		if(j==usedNum){
			work=renkin(item[itemId].recipe[i]);
			usedNum--;
		}else{
			work=-1;
		}
		
		result+=(work!=-1)?work:item[item[itemId].recipe[i]].cost;
	}
	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;
}

C++: #include <iostream>

using namespace std;

struct item
{
	int id;
	int price;
};

struct recipe
{
	int id;
	int size;
	int price;
	int ingre[100];
};

item I[100];
recipe R[100];
string T[100];

int solve(int id, int n, int m);

int main(int argc, char** argv)
{
	
	string name, o, q;
	int n, m, p, k;
	
	while( 1 )
	{
		cin >> n;
		if(n==0) break;
		for(int i=0; i<n; i++)
		{
			cin >> T[i];
			I[i].id = i;
			cin >> I[i].price;
		}
		
		cin >> m;
		for(int i=0; i<m; i++)
		{
			cin >> name;
			for(int j=0; j<n; j++)
			{
				if(T[j]==name)
				{
					R[i].id = j;
				}
			}
			cin >> R[i].size;
			
			R[i].price = 0;
			for(int j=0; j<R[i].size; j++)
			{
				cin >> name;
				for(int k=0; k<n; k++)
				{
					if(T[k]==name)
					{
						R[i].ingre[j] = k;
						R[i].price += I[k].price;
					}
				}
			}
		}
		
		cin >> name;
		for(int i=0; i<n; i++)
		{
			if(name==T[i])
			{
				cout << solve(i, n, m) << endl;
			}
		}
	}
	
	return 0;
}

int solve(int id, int n, int m)
{
	int money = 0;
	bool is_proc = false;
	
	for(int j=0; j<m; j++)
	{
		if(R[j].id == id)
		{
			if(I[id].price >= R[j].price)
			{
				is_proc = true;
				money = 0;
				for(int k=0; k<R[j].size; k++)
				{
					money += solve(R[j].ingre[k], n, m);
				}
			}
			break;
		}
	}
	
	if(is_proc) return money;
	else return I[id].price;
}