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 <bits/stdc++.h>
 
using namespace std;
 
#define MAX 200
#define INF INT_MAX
typedef pair<string, int> P;
typedef vector<string> Vec;
 
int N;
vector<P> vec;
 
int cost(Vec v)
{
    int res = 0;
    for (int i = 0; i < (int)v.size(); i++) {
	for (int j = 0; j < N; j++) {
	    if (vec[j].first == v[i]) {
		res += vec[j].second;
		break;
	    }
	}
    }
    return res;
}
 
int main()
{
    int M, K;
 
    while (cin >> N, N) {
	map<string, Vec> mp;
	P in; 
	vec.clear();
	for (int i = 0; i < N; i++) {
	    cin >> in.first >> in.second;
	    vec.push_back(in);
	}
	
	string str, s, item;
	cin >> M;
	for (int i = 0; i < M; i++) {
	    cin >> str >> K;
	    for (int j = 0; j < K; j++) {
		cin >> s;
		mp[str].push_back(s);
	    }
	}    
 
	map<string, Vec>::iterator it;
	for (int i = 0; i < 2; i++) {
	    for (it = mp.begin(); it != mp.end(); ++it) {
		for (int j = 0; j < N; j++) {
		    if (vec[j].first == it->first) {
			vec[j].second = min(vec[j].second, cost(it->second));
		    }
		}
	    }
	}
	cin >> item;
	int ans = INF;
	for (int i = 0; i < N; i++) {
	    if (vec[i].first == item) {
		ans = vec[i].second;
		break;
	    }
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#define INF 100000000
using namespace std;
int main(){
	int n;
	while(cin >> n,n){
		map<string,int> list;
		for(int i = 0; i < n; i++){
            string d;int a;
            cin >> d >> a;
            list[d] = a;
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; i++){
            string d;int a;
            cin >> d >> a;
            int m = 0;
            for(int j = 0; j < a; j++){
                string dm;
                cin >> dm;
                m += list[dm];
            }
            list[d] = min(list[d],m);
		}
		string ans;
		cin >> ans;
		cout << list[ans] << endl;
	}
	return 0;
}

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

int main() {
	int n;
	while(cin >> n, n) {
		int item;
		string name;
		map<string, int> items;
		for(int i = 0; i < n; ++i) {
			cin >> name >> item;
			items.insert(map<string, int>::value_type(name, item));
		}

		int m;
		cin >> m;
		for(int i = 0; i < m; ++i) {
			int k, sum = 0;
			string o;
			cin >> o >> k;
			for(; k > 0; --k) {
				string q;
				cin >> q;
				sum += items[q];
			}
			items[o] = min(items[o], sum);
		}

		string ans;
		cin >> ans;
		cout << items[ans] << endl;
	}
}

C++: #include <cstdio>
#include <string>
#include <map>
#include <vector>
using namespace std;

map <string, int> item;
map <string, vector<string> > recipe;
int n, m, k, money;
char name[101], o[101], material[101];

int dp(string str);

int main(void)
{
    
    while (1){
        scanf("%d", &n);
        if (n == 0) break;
        for (int i = 0; i < n; i++){
            scanf("%s %d", name, &money);
            item.insert( map<string, int>::value_type( name, money ) );
        }
        
        scanf("%d", &m);
        for (int i = 0; i < m; i++){
            scanf("%s %d", o, &k);
            for (int j = 0; j < k; j++){
                scanf("%s", material);
                recipe[o].push_back(material);
            }
        }
        
        scanf("%s", name);
        printf("%d\n", dp(name));
        
        item.clear();
        recipe.clear();
    }
    
    return (0);
}

int dp(string str)
{
    int ans = 0;
    
    if (recipe[str].size() != 0){
        for (int i = 0; i < recipe[str].size(); i++){
            ans += dp(recipe[str][i]);
        }
    }
    else {
        ans = item[str];
    }
    
    if (item[str] < ans) ans = item[str];
    
    return ans;
}

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

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1e9;

int main() {
    int n;
    while(cin >> n, n) {
        map<string, int> cost;
        for(int i=0; i<n; ++i) {
            string s;
            int p;
            cin >> s >> p;
            cost[s] = p;
        }
        map<string, vector<string>> recipe;
        int m;
        cin >> m;
        for(int i=0; i<m; ++i) {
            string o;
            int k;
            cin >> o >> k;
            for(int j=0; j<k; ++j) {
                string q;
                cin >> q;
                recipe[o].push_back(q);
            }
        }

        for(int i=0; i<n; ++i) {
            for(auto& p : cost) {
                if(recipe.count(p.first) == 0) {
                    continue;
                }
                int sum = 0;
                auto& r = recipe[p.first];
                for(int j=0; j<r.size(); ++j) {
                    sum += cost[r[j]];
                }
                p.second = min(p.second, sum);
            }
        }
        string t;
        cin >> t;
        cout << cost[t] << endl;
    }
}

C++: #include <bits/stdc++.h>  
 
using namespace std;
 
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<string, int> P;

map<string, int> goods;
map<string, int> mp;

int main() {
	int N;
	while(scanf("%d", &N), N) {
		rep(i, N) {
			string s;
			int price;
			cin >> s >> price;
			goods[s] = price; 
		}
		int M;
		scanf("%d", &M);
		rep(i, M) {
			string s;
			int num;
			cin >> s >> num;
			int sum = 0;
			rep(j, num) {
				string g;
				cin >> g;
				sum += goods[g];
			}
			if(sum < goods[s]) goods[s] = sum;
		}
		string ans;
		cin >> ans;
		cout << goods[ans] << endl;
	}
	return 0;
}