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>

using namespace std;

struct item
{
	int id;
	int price;
};

struct recipe
{
	int id;
	int size;
	int price;
	int ingre[100];
};

item I[100];
recipe R[100];
string T[100];

int solve(int id, int n, int m);

int main(int argc, char** argv)
{
	
	string name, o, q;
	int n, m, p, k;
	
	while( 1 )
	{
		cin >> n;
		if(n==0) break;
		for(int i=0; i<n; i++)
		{
			cin >> T[i];
			I[i].id = i;
			cin >> I[i].price;
		}
		
		cin >> m;
		for(int i=0; i<m; i++)
		{
			cin >> name;
			for(int j=0; j<n; j++)
			{
				if(T[j]==name)
				{
					R[i].id = j;
				}
			}
			cin >> R[i].size;
			
			R[i].price = 0;
			for(int j=0; j<R[i].size; j++)
			{
				cin >> name;
				for(int k=0; k<n; k++)
				{
					if(T[k]==name)
					{
						R[i].ingre[j] = k;
						R[i].price += I[k].price;
					}
				}
			}
		}
		
		cin >> name;
		for(int i=0; i<n; i++)
		{
			if(name==T[i])
			{
				cout << solve(i, n, m) << endl;
			}
		}
	}
	
	return 0;
}

int solve(int id, int n, int m)
{
	int money = 0;
	int min = 10000000;
	bool is_proc = false;
	
	for(int j=0; j<m; j++)
	{
		if(R[j].id == id)
		{
			is_proc = true;
			money = 0;
			for(int k=0; k<R[j].size; k++)
			{
				money += solve(R[j].ingre[k], n, m);
			}
			if(money < min) min = money;
			break;
		}
	}
	
	if(is_proc) return min;
	else return I[id].price;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int n, m;
map<string, int> items;
map<string, vector<string> > alc;

map<string, int> memo;
int minPrice(const string& t) {
	vector<string>& mats = alc[t];
	if (!mats.size()) return items[t];
	map<string, int>::iterator it = memo.find(t);
	if (it != memo.end()) return it->second;
	i	nt ap = 0;
	for (unsigned i = 0; i < mats.size(); i++) {
		ap += minPrice(mats[i]);
	}
	return memo[t] = min(items[t], ap);
}

int main () {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			string name;
			int value;
			cin >> name >> value;
			items[name] = value;
		}
		cin >> m;
		for (int i = 0; i < m; i++) {
			string name;
			int k;
			cin >> name >> k;
			vector<string> mats;
			for (int j = 0; j < k; j++) {
				string mat;
				cin >> mat;
				mats.push_back(mat);
			}
			alc[name] = mats;
		}
		string target;
		cin >> target;
		cout << minPrice(target) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <sstream>
using namespace std;

long int match(string,int);
long int value_min(string,long int);

string item_list[100],recipe_to[100];
string recipe_from[100][100];
long int item_value[100];
//long int value_min;

int recipe_num,list_num;
int recipe_to_num[100];


int main(){
//	string recipe_from[100][100];
//	int recipe_num,list_num;//,value_min;
//	int recipe_to_num[100];
	string purpose,item;

	while(cin >> list_num && list_num != 0){
		//		cout << list_num << endl;
		//f[^ÌÇÝÝ
		for(int i=0;i<list_num;i++){
			cin >> item_list[i] >> item_value[i];
			//			cout << item_list[i] << item_value[i] << endl;
		}
		cin >> recipe_num;
		for(int i=0;i<recipe_num;i++){
			cin >> recipe_to[i];
			cin >> recipe_to_num[i];
			for(int j=0;j<recipe_to_num[i];j++){
				cin >> recipe_from[i][j];
			}
		}
		//
		//		for(int i=0;i<recipe_num;i++){
		////			cout << recipe_to[i] << ":";
		//			for(int j=0;j<recipe_to_num[j];j++){
		////				cout << recipe_from[i][j] << " ";
		//			}
		////			cout << endl;
		//		}
		cin >> purpose;
		//		cout << purpose << endl;
		//±±ÜÅf[^ÌÇÝÝ



		cout << value_min(purpose,match(purpose,list_num)) << endl;
	}

}

long int match(string item,int num){
	for(int i=0;i<num;i++){
		if(item == item_list[i]){
			return item_value[i];
		}
	}
}

long int value_min(string item,long int value){
	for(int i=0;i<recipe_num;i++){
		//value = 0;
			if(item == recipe_to[i]){
				long int sum = 0;
				for(int j=0;j<recipe_to_num[i];j++){
					sum += value_min(recipe_from[i][j],match(recipe_from[i][j],list_num));
//					sum += match(recipe_from[i][j],list_num)
				}
				value = (sum > value ? value :  sum);
			}
		}
	return value;
}

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[100];
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) {
			g[i].push_back(conv[O[i]]);
			REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
		}
		
		fill(cost, cost + 100, INF);
		REP(i, N) chmin(cost[conv[S[i]]], P[i]);
		
		while (true) {
			bool update = false;
			REP(i, M) {
				ll sum = 0;
				FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
				update |= chmin(cost[g[i][0]], sum);
			}
			if (!update) break;
		}
		
		cout << cost[conv[T]] << endl;
	}
	return 0;
}