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 <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define rep(I,N) for(int I = 0; I < (int)(N); I++)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
map<string, int> money;
map<string, vector<string> > LESP;
int solve(string make){
	vector<string> item = LESP[make];
	ll mini = INF;
	ll cost = 0;
	rep(i, item.size()){
		if(!LESP[item[i]].empty()){
			//cout << "item" << endl;
			cost += min(money[item[i]], solve(item[i]) );
		}else{
			cost += money[item[i]];
		}
	}
	return cost;
}

int main(void){
	int n, m;
	while(cin >> n, n){
		money.clear();
		LESP.clear();
		string MAKE;
		rep(i, n){
			string s;
			ll cost;
			cin >> s >> cost;
			money[s] = cost;
		}
		cin >> m;
		rep(i, m){
			string s;
			cin >> s;
			int num;
			cin >> num;
			vector<string> v;
			rep(j, num){
				string L;
				cin >> L;
				v.push_back(L);
			}
			LESP[s] = v;
		}
		cin >> MAKE;
		if(m == 0){
			cout << money[MAKE] << endl;
			continue;
		}
		ll ans = solve(MAKE);
		FIN(ans);

	}


	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
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 count = nameList.count( o );
			if( count == 0 ){
				continue;
			}
			int productId = nameList[ o ];
			for( int j = 0; j < k; ++j ){
				string q;
				cin >> q;
				count = nameList.count( q );
				if( count == 0 ){
					recipeList[ productId ] = vector< int >();
					break;
				}
				int materialId = nameList[ q ];
				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[ ansName] ] << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int main()
{
	while(1)
	{
		vector<string> menu[105];
		vector<string> vec;
		int n,m;
		cin >> n;
		if(n==0)break;
		map<string,int> ma;
		for(int i=0;i<n;i++)
		{
			string name;
			int cost;
			cin >> name >> cost;
			ma[name]=cost;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			string s;int k;
			cin >> s;
			vec.pb(s);
			cin >> k;
			for(int j=0;j<k;j++)
			{
				string ss;
				cin >> ss;
				menu[i].pb(ss);
			}
		}
		bool update=true;
		while(update)
		{
			update=false;
			for(int i=0;i<m;i++)
			{
				int sum=0;
				for(int j=0;j<menu[i].size();j++)
				{
					sum+=ma[menu[i][j]];
				}
				if(ma[vec[i]]>sum)
				{
					ma[vec[i]]=sum;
					update=true;
				}
			}
		}
		string out;
		cin >> out;
		cout << ma[out] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#define MAX_N 100
#define MAX_M 100
#define MAX_K 100

using namespace std;

typedef struct _Recipe{
	string NewItemName;
	int SourceItemNumber;
	vector<string> SourceItemName;
}Recipe;

typedef struct _Item{
	string ItemName;
	int Price;
}Item;


int main(void){
	int n, m, min_cost[MAX_N];
	string require;
	Item itm[MAX_N];
	Recipe rcp[MAX_M];
	map<string,int> ItemName_To_Index;
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		for(int i = 0;i < n;i++){
			cin >> itm[i].ItemName >> itm[i].Price;
			min_cost[i] = itm[i].Price;
			ItemName_To_Index.insert(map<string,int>::value_type(itm[i].ItemName,i));
			//cout << "価格 " << itm[i].Price << endl;
		}
		cin >> m;
		for(int i = 0;i < m;i++){
			cin >> rcp[i].NewItemName >> rcp[i].SourceItemNumber;

			for(int j = 0;j < rcp[i].SourceItemNumber;j++){
				string tmp;
				cin >> tmp;
				rcp[i].SourceItemName.push_back(tmp);
			}

		}
		cin >> require;



		for(int i = 0;i < m;i++){
			for(int j = 0;j < m;j++){
				int sum = 0;
				for(int k = 0;k < rcp[j].SourceItemNumber;k++){
					sum += min_cost[ItemName_To_Index[rcp[j].SourceItemName[k]]];
				}
				min_cost[ItemName_To_Index[rcp[j].NewItemName]] = min(sum,min_cost[ItemName_To_Index[rcp[j].NewItemName]]);
			}
		}

		cout << min_cost[ItemName_To_Index[require]] << endl;
		for(int i = 0;i < m;i++){
			rcp[i].SourceItemName.clear();
		}
		ItemName_To_Index.clear();
		/*for(int i = 0;i < n;i++){
			cout << itm[i].ItemName << " 最安値 " << min_cost[i] << endl;
		}*/
	}

	return 0;
}