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 <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <map>
using namespace std;

int main() {
	int n;
	while(cin >> n, n) {
		vector<int> v;
		map<string,int> index;
		bool f[128] = {0};

		for(int i = 0; i < n; i++) {
			string s;
			int c;
			cin >> s >> c;
			v.push_back(c);
			index[s] = i;
		}

		int m;
		int edge[128][128] = {0};
		cin >> m;
		for(int i = 0; i < m; i++) {
			int k;
			string s0, s1;
			cin >> s0 >> k;
			for(int j = 0; j < k; j++) {
				string s1;
				cin >> s1;
				edge[index[s0]][index[s1]] = 1;
//				cout << index[s0] << " " << index[s1] << endl;
				f[index[s0]] = true;
			}
		}

		for(int i = 0; i < v.size(); i++) {
			for(int j = 0; j < v.size(); j++) {
				if(f[j]) {
					int cost = 0;
					for(int k = 0; k < v.size(); k++) {
						if(edge[j][k]) {
							cost += v[k];
						}
					}
					v[j] = min(v[j], cost);
				}
			}
		}

		string want;
		cin >> want;

		cout << v[index[want]] << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <map>
using namespace std;
int main( void )
{
	map < string, int > T;
	int n;
	while ( cin >> n && n != 0 )
	{
		for ( int i = 0; i < n; i++ )
		{
			string item;
			int value;
			cin >> item >> value;

			T[item] = value;
		}

		int m;
		cin >> m;
		for ( int i = 0; i < m; i++ )
		{
			string item;
			int k;
			cin >> item >> k;

			int sum = 0;
			for ( int j = 0; j < k; j++ )
			{
				string additem;
				cin >> additem;
				sum += T[additem];
			}

			T[item] = min( T[item], sum );
		}

		string getitem;
		cin >> getitem;

		cout << T[getitem] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define rep_(v, f, l) for (int v = (f); v < (l); ++v)
#define rep(v, n) rep_(v, 0, n)
#define lep(v, n) rep_(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }

map<string, vector<string> > wglist;
map<string, int> price;
map<string, int> memo;

int get_price(const string& name)
{
    if (memo.find(name) != memo.end()){
        return memo[name];
    }

    int base_price = price[name];
    if (wglist.find(name) == wglist.end()){
        return memo[name] = base_price;
    }

    vector<string> wg = wglist[name];
    int total_price = 0;
    rep(i, wg.size()){
        total_price += get_price(wg[i]);
    }
    return memo[name] = min(total_price, base_price);
}

int main()
{
    int n;
    while (n = in()){
        wglist.clear();
        price.clear();
        memo.clear();
        rep(i, n){
            string name;
            int p;
            cin >> name >> p;
            price[name] = p;
        }
        int wg = in();
        rep(i, wg){
            string name;
            cin >> name;
            int c;
            c = in();
            vector<string> lists;
            rep(j, c){
                string s;
                cin >> s;
                lists.push_back(s);
            }
            wglist[name] = lists;
        }
        string name;
        cin >> name;
        printf("%d\n", get_price(name));
    }
    return (0);
}

C++: #include<bits/stdc++.h>

using namespace std;

int N;
map<string, int> BaseList;


void solve() {
	int m, num, sum;
	string name, tmp;

	/*for (map<string, int>::iterator it = BaseList.begin(); it!= BaseList.end(); ++it) {
		cout << it->first << " " << it->second << endl;
	}*/
	cin >> m;
	for (int i = 0; i < m; ++i) {
		cin >> name >> num;
		sum = 0;
		for (int j = 0; j < num; ++j) {
			cin >> tmp;
			sum += BaseList[tmp];
		}
		if (sum < BaseList[name]) {
			BaseList[name] = sum;
		}
	}
	cin >> name;
	cout << BaseList[name] << endl;
}

int main() {
	int value;
	string str;

	while (cin >> N, N) {
		BaseList.clear();
		for (int i = 0; i < N; ++i) {
			cin >> str >> value;
			BaseList[str] = value;
		}
		solve();
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
	int n,m;
	while(cin>>n&&n){
		map<string,long long> data;
		string s;
		int a;
		for(int i=0;i<n;i++){
			cin>>s>>a;
			data[s]=a;
		}
		cin>>m;
		string wrought[100][100];
		int k,sum;
		for(int i=0;i<m;i++){
			cin>>wrought[i][0];
			sum=0;
			cin>>k;
			for(int j=0;j<k;j++){
				cin>>wrought[i][j+1];
				sum+=data[wrought[i][j+1]];
			}
			if(data[wrought[i][0]]>sum)data[wrought[i][0]]=sum;
		}
		for(int i=0;i<m;i++){
			int sum=0;
			for(int j=0;j<k;j++)sum+=data[wrought[i][j+1]];
			if(data[wrought[i][0]]>sum)data[wrought[i][0]]=sum;
		}
		string ans;
		cin>>ans;
		cout<<data[ans]<<endl;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int func(string name,  map<string,int> &list,  map<string,vector<string> > &renkin){
  if(renkin.find(name)==renkin.end()) return list[name];
  int ret = 0;
  vector<string> mat = renkin[name];
  REP(i,mat.size()){
    ret += min(list[mat[i]], func(mat[i],list,renkin));
  }
  return ret;
}

int main(void){
  int n,m;
  while(cin>>n){
    if(n==0) break;
    map<string,int> list;
    map<string,vector<string> > renkin;
    REP(j,n){
      string i;
      int p;
      cin>>i>>p;
      list[i] = p;
    }
    cin>>m;
    REP(i,m){
      string o;
      int k;
      cin>>o>>k;
      vector<string> q(k);
      REP(j,k) cin>>q[j];
      renkin[o] = q;
    }
    string item;
    cin>>item;
    cout<<func(item,list,renkin)<<endl;
  }
  return 0;
}