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 <vector>
// #include <map>
// #include <algorithm>
// using namespace std;
// typedef long long ll;
// typedef pair<ll, int> P;
// map<string, int> hash;
// int key;
// int findkey(string str) {
//   if (hash.find(str) != hash.end()) return hash[str];
//   return hash[str]=key;
// }

// int n, m, k;
// P p[100];
// ll dp[100];
// vector<int> rec[100];
// string ins;

// ll dfs(int ks) {
//   if (rec[ks].size()==0) return dp[ks];
//   ll sum = 0;
//   for (int i=0; i<rec[ks].size(); i++) {
//     sum += dfs(rec[ks][i]);
//   }
//   if (sum < dp[ks]) dp[ks] = sum;
//   return dp[ks];
// }

// int main() {
//   while (scanf("%d",&n)) {
//     if (!n) break;
//     key=0;
//     for (int i=0; i<n; i++) {
//       cin>>ins>>p[i].first;
//       dp[i]=p[i].first;
//       p[i].second=findkey(ins);
//       key++;
//     }
//     scanf("%d",&m);
//     for (int i=0; i<m; i++) {
//       cin>>ins>>k;
//       int o = findkey(ins);
//       for (int j=0; j<k; j++) {
//         cin>>ins;
//         rec[o].push_back(findkey(ins));
//       }
//     }
//     cin>>ins;
//     int need = findkey(ins);
//     // sort(p, p+n);
//     ll ans = dfs(need);
//     printf("%lld\n",ans);
//   }
// }
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
map<string, int> it;
map<string, vector<string> > rec;
int n, m, p, k;
string str;
int dfs(string s) {
  if (rec[s].size()==0) return it[s];
  int sum = 0;
  for (int i=0; i<rec[s].size(); i++) {
    sum+=dfs(rec[s][i]);
  }
  return it[s]=min(it[s], sum);
}
int main() {
  while (scanf("%d",&n)) {
    if (!n) break;
    it.clear(); rec.clear();
    for (int i=0; i<n; i++) {
      cin>>str>>p;
      it[str] = p;
    }
    scanf("%d",&m);
    for (int i=0; i<m; i++) {
      cin>>str>>k;
      string need = str;
      for (int j=0; j<k; j++) {
        cin>>str;
        rec[need].push_back(str);
      }
    }
    cin>>str;
    cout<<dfs(str)<<endl;
  }
}

C++: #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int n;
int price[100];
vi recipe[100];
int memo[100];
int search(int id)
{
	if(memo[id] >= 0)
		return memo[id];
	if(memo[id] == -2 || recipe[id].empty())
		return memo[id] = price[id];
	
	memo[id] = -2;
	int sum = 0;
	rep(i, recipe[id].size()){
		sum += search(recipe[id][i]);
	}
	return memo[id] = min(price[id], sum);
}

int main()
{
	int n;
	while(scanf("%d", &n), n){
		fill(memo, memo+n, -1);
		
		map<string, int> dict;
		rep(i, n){
			char tmp[128];
			scanf("%s%d", tmp, &price[i]);
			dict[tmp] = i;
		}
		
		int m;
		scanf("%d", &m);
		rep(i, m){
			char tmp[128];
			scanf("%s", tmp);
			int id = dict[tmp];
			int k;
			scanf("%d", &k);
			rep(j, k){
				scanf("%s", tmp);
				recipe[id].push_back(dict[tmp]);
			}
		}
		
		char tmp[128];
		scanf("%s", tmp);
		printf("%d\n", search(dict[tmp]));
	}
	return 0;
}

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

int main(){
	int n;
	while (std::cin>>n,n){
		std::cin >> n;
		std::map<std::string, int>items;
		for (int i = 0; i < n; i++){
			std::string str;
			int a;
			std::cin >> str >> a;
			items.insert(std::make_pair(str, a));
		}
		int renkinn;
		std::cin >> renkinn;
		for (int i = 0; i < renkinn; i++){
			std::string item;
			int sum = 0, a;
			std::cin >> item >> a;
			for (int j = 0; j < a; j++){
				std::string b;
				std::cin >> b;
				sum += items[b];
			}
			if (sum < items[item])items[item] = sum;
		}
		std::string ans;
		std::cin >> ans;
		std::cout << items[ans] << std::endl;
	}
	return 0;
}

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

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

int main(void){
  while(cin >> n && n){
    t.clear();
    for(int i = 0; i < 111; i++) v[i].clear();
    for(int i = 0; i < n; i++){
      string s;
      int a;
      cin >> s >> a;
      t[s] = i;
      c[i] = a;
    }
    cin >> m;
    for(int j = 0; j < m; j++){
      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;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<string,pair<int,int> > > resipi;
vector<pair<string,int> >psi;

int main(){
  int n;
  while(cin>>n,n!=0){
    resipi.clear();
    psi.clear();
    ll cost=0;
    for(int i=0;i<n;i++){
      string str;int ne;cin>>str>>ne;
      psi.push_back(make_pair(str,ne));
    }
    int m;
    cin>>m;
    int ban=0,k=0;
    vector<string> sozai;
    for(int i=0;i<m;i++){
      string renkin;cin>>renkin;
      int snum;cin>>snum;
      for(int j=0;j<snum;j++){
	string s;
	cin>>s;
	sozai.push_back(s);
      }
      resipi.push_back(make_pair(renkin,make_pair(ban,ban+snum)));
      ban+=snum;
    }
    string product;
    cin>>product;
    int flag=0;
    int kane=0;
    for(int i=0;i<resipi.size();i++){
      kane=0;
      for(int j=resipi[i].second.first;j<resipi[i].second.second;j++){
	for(int l=0;l<psi.size();l++){
	  if(sozai[j]==psi[l].first)
	    kane+=psi[l].second;
	}
      }
      for(int j=0;j<psi.size();j++){
	if(resipi[i].first==psi[j].first && kane<psi[j].second){
	  psi[j].second=kane;
	  break;
	}
      }
    }
    for(int i=0;i<psi.size();i++)
      if(psi[i].first==product)
	cout<<psi[i].second<<endl;
  }
return 0;
}