Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
		"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))
	}
}

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>

#define all(a)  (a).begin(),(a).end()
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define exist(s,e) ((s).find(e)!=(s).end())
#define pb push_back
#define range(i,a,b) for(int (i)=(a);(i)<(b);++(i))
#define rep(i,n)  range(i,0,n)
#define dump(x)  cerr << #x << " = " << (x) << endl;

using namespace std;

typedef vector<int> vi;
typedef long long ll;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

const int vmax=110;
int value[vmax];
vector<int> graph[vmax];
string item[vmax];
map<string,int> convert;


int dfs(int v){
	if(graph[v].size()==0) return value[v];
	int sum=0;	
	for(auto &v2:graph[v]) sum+=dfs(v2);
	return min(value[v],sum);
}

int main(void){
	int n;
	while(cin >> n,n){
		// reset
		rep(i,vmax) graph[i].clear();
		convert.clear();

		rep(i,n) cin >> item[i] >> value[i];
		rep(i,n) convert[item[i]]=i;
		int m;
		cin >> m;
		rep(i,m){
			string parent;
			cin >> parent;
			int l;
			cin >> l;
			rep(j,l){
				string child;
				cin >> child;
				graph[convert[parent]].push_back(convert[child]);
			}
		}
		string target;
		cin >> target;
		cout << dfs(convert[target]) << endl;
	}
	return 0;
}

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;
	int 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) {
		items.clear();
		alc.clear();
		memo.clear();
		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<string>
#include<algorithm>
#include<map>
using namespace std;
int main(){

	map<string, int> end;
	int m;
	int cost; string material;
	while (cin >> m, m){
		for (; m > 0; m--){
			cin >> material >> cost;
			end.insert(map<string, int>::value_type(material, cost));
		}
		cin >> m;
		for (; m > 0; m--){
			string sozai; int kaisuu; int sum = 0;
			cin >> sozai >> kaisuu;
			for (; kaisuu > 0; kaisuu--){
				cin >> material;
				sum += end[material];
			}
			if (sum < end[sozai]) end[sozai] = sum;
			sum = 0;
		}
		string in;
		cin >> in;
		cout << end[in] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#define mod 1000000007
using namespace std;
map<string,int> cost,nz;
map<string,vector<string> > parent,child;
vector<string> v;
void DFS(string s){
	int ans=0;
	for(int i=0;i<child[s].size();i++){
		ans+=cost[child[s][i]];
	}
	ans=min(ans,cost[s]);
	if(child[s].size()>0)cost[s]=ans;
	for(int i=0;i<parent[s].size();i++){
		string to=parent[s][i];
		nz[to]--;
		if(nz[to]==0){
			DFS(to);
		}
	}
}
int main(){
	int n,m;
	while(cin>>n&&n){
		cost.clear();
		nz.clear();
		child.clear();
		parent.clear();
		v.clear();
		for(int i=0;i<n;i++){
			string s;int a;
			cin>>s>>a;
			cost[s]=a;
			nz[s]=0;
			v.push_back(s);
		}
		cin>>m;
		for(int i=0;i<m;i++){
			string s;int k;
			cin>>s>>k;
			nz[s]=k;
			for(int j=0;j<k;j++){
				string r;cin>>r;
				child[s].push_back(r);
				parent[r].push_back(s);
			}
		}
		for(int i=0;i<v.size();i++){
			if(nz[v[i]]==0)DFS(v[i]);
		}
		string s;cin>>s;
		cout<<cost[s]<<endl;
	}
	return 0;
}

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