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 <string>
#include <queue>
#include <map>

std::map< std::string, unsigned long int > mp;
std::map< std::string, std::queue< std::string > > recipe;

int n, m;
std::string t;

unsigned long int wrought(std::string s) {
    if ( recipe.find(s) == recipe.end() ) {
        return mp[s];
    } else {
        std::queue< std::string > q = recipe[s];
        unsigned long int res = 0;
        while (!q.empty()) {
            std::string t = q.front();
            q.pop();
            res += wrought( t );
        }
        return std::min(res, mp[s]);
    }
}

void solve(std::string t) {
    unsigned long int res = std::min(mp[t], wrought( t ));
    std::cout << res << std::endl;
}

int main() {
    while (std::cin >> n, n) {
        // initialize map
        mp.clear();
        recipe.clear();

        // get input
        for (int i=0; i<n; i++) {
            std::string s;
            unsigned long int price;
            std::cin >> s >> price;
            mp[s] = price;
        }
        std::cin >> m;
        for (int i=0; i<m; i++) {
            std::string obj;
            int k;
            std::cin >> obj >> k;
            for (int j=0; j<k; j++) {
                std::string q;
                std::cin >> q;
                recipe[obj].push( q );
            }
        }
        std::string t;
        std::cin >> t;
        solve( t );
    }
    return 0;
}

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

map<string, int> v;
map<string, vector<string> > l;

int main()
{
	int n;
	while (scanf("%d", &n), n){
		v.clear();
		l.clear();
		
		for (int i = 0; i < n; i++){
			string str;
			int p;
			cin >> str >> p;
			v[str] = p;
		}
		
		int m;
		scanf("%d", &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;
				l[o].push_back(q);
			}
		}
		
		while (true){
			bool update = false;
			
			typedef map<string, vector<string> >::iterator ite;
			for (ite i = l.begin(); i != l.end(); ++i){
				int sum = 0;
				for (int j = 0; j < (*i).second.size(); j++){
					sum += v[(*i).second[j]];
				}
				if (v[(*i).first] > sum){
					v[(*i).first] = sum;
					update = true;
				}
			}
			if (!update) break;
		}
		
		string t;
		cin >> t;
		cout << v[t] << endl;
	}
	
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

vector<pair<string,int> > items;
vector<pair<string,vector<string> > > renkins;

int main() {
	int n,m,k;
	string i,o;
	while(n=in()){
		int j;
		items.clear();
		for(j=0;j<n;j++){
			string s;
			int c;
			cin>>s>>c;
			items.push_back(mp(s,c));
		}
		m=in();
		renkins.clear();
		pair<string,vector<string> > renkin;
		int x;
		for(j=0;j<m;j++){
			renkin.second.clear();
			cin>>renkin.first>>k;
			string str;
			for(x=0;x<k;x++){
				cin>>str;
				renkin.second.push_back(str);
			}
			renkins.push_back(renkin);
		}
//for(x=0;x<renkins.size();x++){
//	int z;
//	cout<<renkins[x].first<<" : ";
//	for(z=0;z<renkins[x].second.size();z++){
//		cout<<renkins[x].second[z];
//	}
//	cout<<endl;
//}
		bool hoge=true;
		while(hoge){
			hoge=false;
			for(j=0;j<m;j++){
				int sum=0;
				for(x=0;x<renkins[j].second.size();x++){
					int y;
					for(y=0;y<n;y++){
						if(items[y].first==renkins[j].second[x]){
							sum+=items[y].second;
						}
					}
				}
				for(x=0;x<n;x++){
					if(items[x].first==renkins[j].first){
						if(items[x].second>sum){
							items[x].second=sum;
							hoge=true;
						}
					}
				}
			}
		}
		string st;
		cin>>st;
//for(x=0;x<n;x++){
//	cout<<items[x].first<<" "<<items[x].second<<endl;
//}
		for(x=0;x<n;x++){
			if(items[x].first==st){
				cout<<items[x].second<<endl;
			}
		}
	}
	return 0;
}

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

long long int updateValue( int n, vector< long long int >& valueList, vector< vector< int > >& recipeList, vector< bool >& updatedList )
{
	if( updatedList[ n ] ){
		return valueList[ n ];
	}
	updatedList[ n ] = true;
	vector< int > recipe = recipeList[ n ];
	if( recipe.empty() ){
		return valueList[ n ];
	}
	long long int value = 0;
	for( int i = 0; i < (int)recipe.size(); ++i ){
		int id = recipe[ i ];
		value += updateValue( id, valueList, recipeList, updatedList );
	}
	if( value < valueList[ n ] ){
		valueList[ n ] = value;
	}
	return valueList[ n ];
}

int main()
{
	int n;
	while( cin >> n && n ){
		int id = 0;
		map< string, int > nameList;
		vector< long long int > valueList( n, 0 );
		for( int i = 0; i < n; ++i ){
			string s;
			int v;
			cin >> s >> v;
			int d = id++;
			nameList.insert( make_pair( s, d ) );
			valueList[ d ] = v;
		}
		int m;
		vector< vector< int > > recipeList( n, vector< int >() );
		cin >> m;
		for( int i = 0; i < m; ++i ){
			string o;
			int k;
			cin >> o >> k;
			int productId = nameList.find( o )->second;
			for( int j = 0; j < k; ++j ){
				string q;
				cin >> q;
				int materialId = nameList.find( q )->second;
				recipeList[ productId ].push_back( materialId );
			}
		}
		vector< bool > updatedList( n, false );
		for( int i = 0; i < n; ++i ){
			updateValue( i, valueList, recipeList, updatedList );
		}

		string ansName;
		cin >> ansName;
		cout << valueList[ nameList.find( ansName )->second ] << endl;
	}
	return 0;
}