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 <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);
map<string, int> money;
map<string, vector<string> > LESP;
int solve(string make){
	vector<string> item = LESP[make];
	ll mini = INF;
	ll cost = 0;
	if(item.size() == 0)return money[make];
	rep(i, item.size()){
		if(!LESP[item[i]].empty()){
			//cout << "item" << endl;
			cost += min(money[item[i]], solve(item[i]) );
		}else{
			cost += money[item[i]];
		}
	}
	return cost;
}

int main(void){
	int n, m;
	while(cin >> n, n){
		money.clear();
		LESP.clear();
		string MAKE;
		rep(i, n){
			string s;
			ll cost;
			cin >> s >> cost;
			money[s] = cost;
		}
		cin >> m;
		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;
		ll ans = solve(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 <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[itemId].cost)?work:item[itemId].cost;
	}
	if(i==0) result=item[itemId].cost;

	return result;
}

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

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
using namespace std;
typedef struct{
  vector<string> sozai;
  int money;
}BUTU;
pair<string,int> P[1002];
BUTU butu[1024];
int n,p,m,a,b;
string s,ss,mokuhyou;
int tansaku(string now){
  for(int i=0;i<n;i++){
    if(P[i].first==now){
      return P[i].second;
    }
  }
  return -1;
}
int check(string now){
  int banti=tansaku(now);
  int f=butu[banti].sozai.size();
  int cnt=0;
  if(f==0){
    return butu[banti].money;
  }
  else{
    for(int i=0;i<f;i++){
      if(tansaku(butu[banti].sozai[i])!=-1){
	cnt+=check(butu[banti].sozai[i]);
      }
      else{
	return butu[banti].money;
      }
    }
    return min(cnt,butu[banti].money);
  }
  return -1;
}
main(){
  while(1){
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++){
      cin>>s>>p;
      butu[i].money=p;
      P[i].first=s;
      P[i].second=i;
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>s>>a;
      b=tansaku(s);
      for(int j=0;j<a;j++){
	cin>>ss;
	butu[b].sozai.push_back(ss);
      }
    }
    cin>>mokuhyou;
    cout<<check(mokuhyou)<<endl;
  }
}

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

map<string, int> item;
map<string, vector<string>> recipe;
int rec(string s) {
    if (!recipe.count(s)) return item[s];
    int cost = 0;
    for (string t: recipe[s]) cost += rec(t);
    return min(item[s], cost);
}

int main() {
    int n;
    while (cin >> n, n) {
        item.clear();
        while (n--) {
            string s; int p; cin >> s >> p; item[s] = p;
        }
        int m; cin >> m;
        recipe.clear();
        while (m--) {
            string s; int k; cin >> s >> k;
            while (k--) {
                string t; cin >> t; recipe[s].emplace_back(t);
            }
        }
        string r; cin >> r;
        cout << rec(r) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include <map>
using namespace std;
int search(string, pair<string, int>*, pair<string, vector<string> >*);
int n;
int m,si;
int main()
{
	
	while (true){
		pair<string, int>p[101];
		pair<string, vector<string> >chan[101];
		cin >> n;
		if (n == 0){
			break;
		}
		for (int i = 0; i < n; i++){
			cin >> p[i].first >> p[i].second;
		}

		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> chan[i].first >> si;
			for (int i2 = 0; i2 < si; i2++){
				string s;
				cin >> s;
				chan[i].second.push_back(s);
			}
		}
		string s;
		cin >> s;
		int ans = search(s,p,chan);
		cout << ans << endl;
	}
}
int search(string s, pair<string, int>*p, pair<string, vector<string> >*chan){
	int num = 0;
	bool flag = true;
	for (int i = 0; i < m; i++){
		if (chan[i].first == s){
			flag = false;
			for (int i2 = 0; i2 < chan[i].second.size(); i2++){
				num += search(chan[i].second[i2],p,chan);
			}
		}
	}
	if (flag){
		for (int i = 0; i < n; i++){
			if (p[i].first == s){
				num += p[i].second;
			}
		}
	}
	return num;
}

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

struct RECIPE{
	string o;
	int k;
	string q[100];
};

RECIPE recipe[100];
int n;
pair<string, int> ip[100];
int m;

int func(string order){

	int x, y;
	bool flg = false;
	for (int i = 0; i < m; i++){
		if (recipe[i].o == order){
			flg = true;
			x = i;
			break;
		}
	}
	for (int i = 0; i < n; i++){
		if (ip[i].first == order){
			y = i;
			break;
		}
	}
	int sum = 0;
	if (flg){
		for (int i = 0; i < recipe[x].k; i++){
			sum += func(recipe[x].q[i]);
		}
	}
	else{
		for (int i = 0; i < n; i++){
			if (ip[i].first == order)
				return ip[i].second;
		}
	}

	return min(sum, ip[y].second);
}

int main(void){

	while (cin >> n && n != 0){
		string order;

		for (int i = 0; i < n; i++)
			cin >> ip[i].first >> ip[i].second;
		cin >> m;
		for (int i = 0; i < m; i++){

			cin >> recipe[i].o >> recipe[i].k;
			for (int j = 0; j < recipe[i].k; j++)
				cin >> recipe[i].q[j];
		}

		cin >> order;
		cout << func(order) << endl;
	}
}