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 <cstdio>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
char buf[128];
vector<string> items;
map<string, int> info;
map<string, vector<string> > recipe;
int main()
{
	while (true) {
		int n;
		scanf("%d", &n);
		if (n == 0) break;
		rep(i, n) {
			int c;
			scanf("%s %d", buf, &c);
			info[string(buf)] = c;
			items.push_back(string(buf));
		}
		int m;
		scanf("%d", &m);
		rep(i, m) {
			int k;
			scanf("%s %d", buf, &k);
			string item(buf);
			rep(j, k) {
				scanf("%s", buf);
				recipe[item].push_back(string(buf));
			}
		}
		scanf("%s", buf);
		string wish(buf);

		while (true) {
			bool f = true;
			rep(i, items.size()) {
				if (recipe.find(items[i]) != recipe.end()) {
					int c = 0;
					rep(j, recipe[items[i]].size())
						c += info[recipe[items[i]][j]];
					if (c < info[items[i]]) {
						f = false;
						info[items[i]] = c;
					}
				}
			}
			if (f) break;
		}
		printf("%d\n", info[wish]);
	}
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
int n;

struct item{
    string name;
    int cost;
    vector<string> used;
    int size;
} ;

item list[100];
int memo[100];

int getCost(int v)
{
    if(memo[v]!=-1)return memo[v];
    int i, j;
    int ans;
    
    if (list[v].size == 0){
        return (list[v].cost);
    }
    else {
        ans = 0;
        for (i = 0; i < list[v].size; i++){
            for (j = 0; j < n; j++){
                if (list[v].used[i]== list[j].name){
                    ans += getCost(j);
                }
            }
        }
        list[v].cost = min(list[v].cost,ans);
    }
    return (list[v].cost);
}

int main(void)
{
    int i, j, k;
    int m, kk;
    string key, temp;
    int look, sum;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        for(i=0;i<n;i++)memo[i]=-1;
        for (i = 0; i < n; i++){
            cin >> list[i].name >> list[i].cost;
            list[i].size = 0;
        }
        
        cin >> m;
        for (i = 0; i < m; i++){
            cin >> key;
            for (j = 0; j < n; j++){
                if (key==list[j].name){
                    look = j;
                }
            }
            cin >> kk;
            list[look].size = kk;
            for (j = 0; j < kk; j++){
                cin >> temp;
                for (k = 0; k < n; k++){
                    if (temp==list[k].name){
                        list[look].used[j]=temp;
                    }
                }
            }
        }
        cin >> key;
        for (j = 0; j < n; j++){
            if (strcmp(key, list[j].name) == 0){
                look = j;
            }
        }
        
        cout << getCost(look);
        
    }
    
    return (0);
}

C++: #include<iostream>
#include<map>
#include<cstdio>
#include<string>
using namespace std;
int main(){
	int n,m,en,cnt,a,b,c;
	string str,str2,str3;
	map<string,int> data;
	
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> str >> en;
		data[str]=en;
	}
	cin >> m;
	for(int i=0;i<m;i++){
		cnt = 0;
		cin >> str >> a;
		for(int j=0;j<a;j++){
			cin >> str2;
			cnt += data[str2];
		}
		if(data[str] > cnt){
			data[str] = cnt;
		}
	}
	cin >> str;
	cout << data[str] << endl;	
}

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


int			price[100];
bool		decided[100];
vector<int>	recipe[100];

int dfs(int index)
{
	if(decided[index])
	{
		return price[index];
	}
	if(recipe[index].empty())
	{
		decided[index] = true;
		return price[index];
	}

	int newprice=0;
	for(size_t i=0; i<recipe[index].size(); i++)
	{
		newprice+=dfs(recipe[index][i]);
	}
	price[index] = (newprice<price[index]?newprice:price[index]);

	decided[index] = true;
	return price[index];
}

int main()
{
	int N,M,p,l,idx;
	string s;
	while(cin >> N, N)
	{
		map<string,int> s2i;
		for(int i=0; i<N; i++)
		{
			cin >> s >> p;
			s2i[s] = i;

			price[i]   = p;
			decided[i] = false;
			recipe[i].clear();
		}

		cin >> M;
		for(int i=0; i<M; i++)
		{
			cin >> s >> l;
			idx = s2i[s];
			while(l--)
			{
				cin >> s;
				recipe[idx].push_back(s2i[s]);
			}
		}

		cin >> s;
		cout << dfs(s2i[s]) << endl;		
	}

	return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<57;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int N, M;
ll cost[200];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
	while (cin >> N, N) {
		REP(i, N) cin >> S[i] >> P[i];
		cin >> M;
		REP(i, M) {
			cin >> O[i] >> K[i];
			REP(j, K[i]) cin >> Q[i][j];
		}
		cin >> T;
		
		map<string, int> conv;
		REP(i, N) conv[S[i]] = conv.size() - 1;
		REP(i, M) {
			if (conv.find(O[i]) == conv.end()) conv[O[i]] = conv.size() - 1;
			REP(j, K[i]) if (conv.find(Q[i][j]) == conv.end()) conv[Q[i][j]] = conv.size() - 1;
		}
		
		REP(i, M) {
			g[i].push_back(conv[O[i]]);
			REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
		}
		
		fill(cost, cost + conv.size(), INF);
		REP(i, N) chmin(cost[conv[S[i]]], P[i]);
		
		REP(i, M - 1) {
			REP(j, M) {
				ll sum = 0;
				FOR(k, 1, g[j].size()) sum += cost[g[j][k]];
				chmin(cost[g[j][0]], sum);
			}
		}
		
		cout << cost[conv[T]] << endl;
	}
	return 0;
}