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<map>
#include<string>
#include<vector>
#include<stdlib.h>

void listen_price(std::map<std::string,int> &);
void listen_recipe(std::map<std::string,std::vector<std::string> > &);
void listen_choicedItem(std::string &);
void echo_bestPrice(std::string &,std::map<std::string,int> &,std::map<std::string,std::vector<std::string> >&);
int  calc_bestPrice(std::string &,std::map<std::string,int> &,std::map<std::string,std::vector<std::string> >&);

void wroughtGoldMaster();

//-------------------main------------------------------

int main()
{
	for(;1;)
	{
		wroughtGoldMaster();//終了処理は関数内のexitで行われる
	}
	return 0;
}
void wroughtGoldMaster()
{
	std::map<std::string,int> price;
	listen_price(price);
	
	std::map<std::string,std::vector<std::string> > recipe;//本来はmultimapを使うべきかもしれないが練習のため
	listen_recipe(recipe);
	
	std::string choicedItem;
	listen_choicedItem(choicedItem);
	echo_bestPrice(choicedItem,price,recipe);
}

//----------------------------------------------------

void listen_price(std::map<std::string,int> &price)
{
	using namespace std;
	
	int kind_num;
	cin>>kind_num;
	if(!kind_num)
	{
		exit(0);//入力の終わり
	}
	
	string tmp_string;
	int tmp_value;
	for(int roop=0;roop<kind_num;roop++)
	{
		cin>>tmp_string>>tmp_value;
		price[tmp_string]=tmp_value;
	}
}

void listen_recipe(std::map<std::string,std::vector<std::string> >&recipe)
{
	using namespace std;
	int kind_num;
	cin>>kind_num;
	
	string tmp_string;
	string tmp_string_forItem;
	int itemCount_num;
	for(int roop=0;roop<kind_num;roop++)
	{
		cin>>tmp_string>>itemCount_num;
		for(int itemroop=0;itemroop<itemCount_num;itemroop++)
		{
			cin>>tmp_string_forItem;
			recipe[tmp_string].push_back(tmp_string_forItem);
		}
	}
}

void listen_choicedItem(std::string &choicedItem)
{
	using namespace std;
	
	cin>>choicedItem;
}

void echo_bestPrice(std::string &choicedItem,std::map<std::string,int> &price,std::map<std::string,std::vector<std::string> >&recipe)
{
	using namespace std;
	
	cout<<calc_bestPrice(choicedItem,price,recipe)<<endl;//calc_bestPrice:再帰的にレシピを探索して各品目の最安コストを探索するための関数
}

int calc_bestPrice(std::string &item,std::map<std::string,int> &price,std::map<std::string,std::vector<std::string> >&recipe)
{
	using namespace std;
	
	vector<string>::iterator itr;
	int sum_compCost;
	if(recipe.find(item)==recipe.end())
	{
		return price[item];
	}
	else
	{
		sum_compCost=0;
		for(itr=recipe[item].begin();itr<recipe[item].end();++itr)
		{
			sum_compCost+=calc_bestPrice(*itr,price,recipe);
		}
	}
	
	if(sum_compCost<price[item])
	{
		return sum_compCost;
	}
	else
	{
		return price[item];
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
map<string,int>m;
vector<string>v[100];
int c;
int dfs(string s){
  int res=m[s],p=-1,res2=0;
  for(int i=0;i<c;i++){
    if(v[i][0]==s)p=i;
  }
  if(p!=-1)for(int i=1;i<v[p].size();i++){
    res2+=dfs(v[p][i]);
  }
  else return res;
  //cout<<s<<' '<<res<<' '<<res2;
  return min(res,res2);
}
int main(){
  int n;
  while(cin>>n,n){
    c=0;
    for(int i=0;i<100;i++)v[i].clear();
      m.clear();
    while(n--){
      string t;
      int p;
      cin>>t>>p;
      m[t]=p;
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
      string t;
      cin>>t;
      v[i].push_back(t);
      int p;c++;
      cin>>p;
      while(p--){
        cin>>t;
        v[i].push_back(t);
      }
    }
    string t;
    cin>>t;
    cout<<dfs(t)<<endl;
  }
}

C++: #include <cstdio>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
char buf[128];
vector<string> items;
map<string, int> info;
map<string, vector<string> > recipe;
int main()
{
	while (true) {
		int n;
		scanf("%d", &n);
		if (n == 0) break;
		rep(i, n) {
			int c;
			scanf("%s %d", buf, &c);
			string item(buf);
			if (info.find(item) != info.end())
				info[item] = min(c, info[item]);
			else {
				info[item] = c;
				items.push_back(item);
			}
		}
		int m;
		scanf("%d", &m);
		rep(i, m) {
			int k;
			scanf("%s %d", buf, &k);
			string item(buf);
			rep(j, k) {
				scanf("%s", buf);
				recipe[item].push_back(string(buf));
			}
		}
		scanf("%s", buf);
		string wish(buf);

		while (true) {
			bool f = true;
			rep(i, items.size()) {
				if (recipe.find(items[i]) != recipe.end()) {
					int c = 0;
					rep(j, recipe[items[i]].size())
						c += info[recipe[items[i]][j]];
					if (c < info[items[i]]) {
						f = false;
						info[items[i]] = c;
					}
				}
			}
			if (f) break;
		}
		printf("%d\n", info[wish]);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1; i>=0; i--)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n;
  while(cin >> n && n) {
    map<string,int> m;
    rep(i,n) {
      string s;
      int x;
      cin >> s >> x;
      m[s]=x;
    }
    cin >> n;
    map<string, vector<string> > m2;
    rep(i,n) {
      string s;
      cin >> s;
      int k;
      cin >> k;
      vector<string> v(k);
      rep(j,k) cin >> v[j];
      m2[s]=v;
    }
    while(1) {
      bool update=false;
      tr(it,m2) {
	vector<string> v=it->S;
	int sum=0;
	rep(i,v.size()) sum+=m[v[i]];
	if(m[it->F]>sum) {
	  update=true;
	  m[it->F]=sum;
	}
      }
      if(!update) break;
    }
    string s;
    cin >> s;
    pr(m[s]);
  }
  return 0;
}