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 <stdio.h>
#include <string.h>

struct data{
	char name[101];		//¼O
	int price;			//li
	int ren;			//BàÅ«é©
	int renkin[101];	//BàÅg¤itemÌzñÌY¦[0]ÉÍÂðüêÄ¨­
};
data item[101];
int visit[101];			//»ÌACeðêxKê½©Ç¤©B

int func(int number){
	int c=item[number].renkin[0];
	int ans=0;
	visit[number]=1;
//	printf("%s\n",item[number].name);
	if(item[number].renkin[0]==0){
		return item[number].price;
	}
	for(int i=1;i<=c;i++){
		if(visit[item[number].renkin[i]]==1){
		//	printf("visited!! %s %d\n",item[item[number].renkin[i]].name,item[item[number].renkin[i]].price);
			ans+=item[item[number].renkin[i]].price;
		}
		else if(item[item[number].renkin[i]].ren==0){
//			printf("Can't!!   %s %d\n",item[item[number].renkin[i]].name,item[item[number].renkin[i]].price);
			ans+=item[item[number].renkin[i]].price;
		}
		else {
//			printf("renkin!!  %s %d\n",item[item[number].renkin[i]].name,item[item[number].renkin[i]].price);
			ans+=func(item[number].renkin[i]);
		}
	}
	if(ans<item[number].price)
		item[number].price=ans;
	
	return item[number].price;
}
			

int main(void){
	int n,i,j,l;
	while(scanf("%d",&n),n){
		
		int m,k,t;
		char temp[101];
		for(i=0;i<n;i++){
			scanf("%s %d",item[i].name,&item[i].price);
		}
		//±±©çBà
		scanf("%d",&m);
		for(i=0;i<m;i++){
			
			//Å«éàÌ
			scanf("%s",temp);
			for(j=0;j<n;j++){
				if(!(strcmp(item[j].name,temp))){
					item[j].ren=1;
					break;
				}
			}
			t=j;
//			puts(item[t].name);
			//´¿
			scanf("%d",&k);
			item[t].renkin[0]=k;
			for(j=0;j<k;j++){
				scanf("%s",temp);
				for(l=0;l<n;l++){
					if(!(strcmp(item[l].name,temp))){
						item[t].renkin[j+1]=l;
						break;
					}
				}
			}
		}
		scanf("%s",temp);
		for(i=0;i<n;i++){
			if(!(strcmp(item[i].name,temp))){
				break;
			}
		}
		//±±ÜÅÇÝÝ
		
		
		//wè³ê½ACeÅÄAµÄ¢­
		//©ªªoÄ«½ç¤BBàÅ«È¢B»±ðKê½©Å»fH
	//	puts("ÄA");
		printf("%d\n",func(i));
		
		for(i=0;i<101;i++){
			visit[i]=0;
			item[i].ren=0;
			item[i].price=0;
		}
	}
}

C++: // 0201.cpp : テ」ツδ。テ」ツつ、テ」ツδウ テ」ツδ療」ツδュテ」ツつクテ」ツつァテ」ツつッテ」ツδ?テ」ツδ陛」ツつ。テ」ツつ、テ」ツδォテ」ツ?ァテ」ツ?凖」ツ??

//#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<map>

using namespace std;

map<string,int> s1;
map<string,vector<string> > s2;
int INF=100000000;
int gold(string s){
	int res=0;
	int res2=INF;
	res=s1[s];
	if(s2[s].size()>0)res2=0;
	for(int i=0;i<s2[s].size();i++){
		res2+=gold(s2[s][i]);
	}
	res=min(res,res2);
	return res;
}

int main(void){
	int n,m;
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		s1.clear();
		s2.clear();
		for(int i=0;i<n;i++){
			string str;
			int c;
			cin >> str >> c;
			s1[str]=c;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			string str;
			int k;
			cin >> str >> k;
			for(int j=0;j<k;j++){
				string s;
				cin >> s;
				s2[str].push_back(s);
			}
		}
		string s3;
		cin >> s3;
		printf("%d\n",gold(s3));
	}
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include <map>
#include<algorithm>
using namespace std;
int search(string, pair<string, int>*, pair<string, vector<string> >*);
int n;
int m,si;
int main()
{
	
	while (true){
		pair<string, int>p[101];
		pair<string, vector<string> >chan[101];
		cin >> n;
		if (n == 0){
			break;
		}
		for (int i = 0; i < n; i++){
			cin >> p[i].first >> p[i].second;
		}

		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> chan[i].first >> si;
			for (int i2 = 0; i2 < si; i2++){
				string s;
				cin >> s;
				chan[i].second.push_back(s);
			}
		}
		string s;
		cin >> s;
		int ans = search(s,p,chan);
		cout << ans << endl;
	}
}
int search(string s, pair<string, int>*p, pair<string, vector<string> >*chan){
	int num = 0;
	bool flag = true;
	for (int i = 0; i < m; i++){
		if (chan[i].first == s){
			flag = false;
			int o;
	
			for (int i2 = 0; i2 < chan[i].second.size(); i2++){
			for (int i3 = 0; i3 < n; i3++){
				if (p[i3].first == chan[i].second[i2]){
					 o=p[i3].second;
					 break;
				}
			}
				num += min(search(chan[i].second[i2],p,chan),o);
			}
		}
	}
	if (flag){
		for (int i = 0; i < n; i++){
			if (p[i].first == s){
				num += p[i].second;
			}
		}
	}
	return num;
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int a,s[101],d,f[101],g=10000001,h[101],q,w,e;
char z[101][101],x[101][101],c[101][101][101],v[101];
int main(void)
{
	int i,j,k;
	while(1){
		scanf("%d",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			scanf("%s %d",z[i],&s[i]);
		}
		for(i=0;i<a-1;i++){
			for(j=i+1;j<a;j++){
				if(strcmp(z[i],z[j])==0){
					if(s[i]<s[j]) s[j]=s[i];
					else s[i]=s[j];
				}
			}
		}
		scanf("%lld",&d);
		for(i=0;i<d;i++){
			scanf("%s %lld",x[i],&f[i]);
			for(j=0;j<f[i];j++){
				scanf("%s",c[i][j]);
			}
			h[i]=0;
		}
		scanf("%s",v);
		for(i=0;i<a;i++) if(strcmp(v,z[i])==0) if(g>s[i]) g=s[i];
		for(i=0;i<d;i++){
			for(j=0;j<d;j++){
				for(k=0;k<f[j];k++){
					if(strcmp(x[i],c[j][k])==0) h[j]++;
				}
			}
		}
		if(d!=0){
			w=0;
			while(1){
				for(i=0;i<d;i++){
					q=0;
					e=0;
					if(h[i]==0){
						w++;
						for(j=0;j<a;j++){
							for(k=0;k<f[i];k++){
								if(strcmp(z[j],c[i][k])==0){
									q+=s[j];
									break;
								}
							}
						}
						for(j=0;j<a;j++){
							if(strcmp(z[j],x[i])==0){
								if(s[j]>q){
									s[j]=q;
									q=0;
									break;
								}
							}
						}
						if(q!=0){
							s[a]=q;
							strcpy(z[a],x[i]);
							a++;
						}
					}
					h[i]--;
				}
				if(w==d) break;
			}
		}
		for(i=0;i<a;i++){
			if(strcmp(v,z[i])==0){
				printf("%lld\n",s[i]);
				break;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>

using namespace std;

map<string,int> minCosts;
map<string,int> value;
map<string,vector<string> > gousei;

int resolveMinCost(string name){
	if(minCosts.find(name)!=minCosts.end())
		return minCosts[name];

	int cost=value[name];
	if(gousei.find(name)!=gousei.end()){
		int sumCost=0;
		for(int i = 0; i < gousei[name].size(); i++){
			sumCost+=resolveMinCost(gousei[name][i]);
		}
		cost=min(cost,sumCost);
	}

	minCosts[name]=cost;
	return cost;
}

int main(){

	int n;
	while(cin>>n&&n!=0){
		for(int i =0; i < n; i++){
			string str;
			cin>>str;
			int val;
			cin>>val;
			value[str]=val;
		}
		int m;
		cin>>m;
		for(int i = 0; i < m; i++){
			string name;
			int k;
			cin>>name;
			cin>>k;
			vector<string> v;
			for(int j = 0; j < k; j++){
				string str;
				cin>>str;
				v.push_back(str);
			}
			gousei[name]=v;
		}
		string item;
		cin>>item;

		cout<<resolveMinCost(item)<<endl;

		gousei.clear();
		value.clear();
		minCosts.clear();
	}
	
	return 0;
}