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 <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    while(cin >> n, n){
        int id = 0; map<string, int> M;
        vector<int> dp(n), dp2(n, INF<int>);
        for (int i = 0; i < n; ++i) {
            string s; int x;
            cin >> s >> x;
            dp[id] = x;
            M[s] = id++;
        }
        int m;
        cin >> m;
        vector<int> deg(n);
        vector<vector<int>> G(n);
        for (int i = 0; i < m; ++i) {
            string s; int a;
            cin >> s >> a;
            for (int j = 0; j < a; ++j) {
                string t; cin >> t;
                G[M[t]].emplace_back(M[s]);
                deg[M[s]]++;
            }
        }
        queue<int> Q;
        for (int i = 0; i < n; ++i) {
            if(!deg[i]) Q.emplace(i);
        }
        while(!Q.empty()){
            int i = Q.front(); Q.pop();
            for (auto &&j : G[i]) {
                if(dp2[j] == INF<int>) dp2[j] = min(dp[i], dp2[i]);
                else dp2[j] += min(dp[i], dp2[i]);
                deg[j]--;
                if(!deg[j]) Q.emplace(j);
            }
        }
        string s;
        cin >> s;
        cout << min(dp[M[s]], dp2[M[s]]) << "\n";
    }
    return 0;
}

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;
  }
}