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<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
int keisan(char[100]);
struct SS{
	char name[100];
	int cnt;
	char zairyo[100][100];
};
struct AAA{
	char na[100];
	int cost;
};
SS recipe[100];
AAA IP[100];
int n,m;
char target[100];
int main()
{
	while(1){
		scanf("%d",&n);
		if(n == 0) break;
		for(int i=0;i<n;i++) 
			scanf("%s %d",IP[i].na,&IP[i].cost);
		scanf("%d",&m);////n
		for(int i=0;i<m;i++){//n
			scanf("%s %d", recipe[i].name, &recipe[i].cnt);
			for(int j=0; j<recipe[i].cnt; j++){
				scanf("%s",recipe[i].zairyo[j]);
			}
		}
		scanf("%s",target);
	//////////////////////////
	/*printf("%d\n",n);
	for(int i=0;i<n;i++) 
		printf("%s %d\n",IP[i].na,IP[i].cost);
	printf("%d\n",m);
	for(int i=0;i<m;i++){//n
		printf("%s %d ", recipe[i].name, recipe[i].cnt);
		for(int j=0; j<recipe[i].cnt; j++){
			printf("%s ",recipe[i].zairyo[j]);
		}
		printf("\n");
	}
	printf("%s\n",target);*/
	/////////////////////////
		printf("%d\n",keisan(target));
	}
	return 0;
}

int keisan(char name[100]/*,int money*/)
{
	int money = 0;//
	for(int i=0;i<m;i++){
		//printf("Money=%d\n",money);
		if(strstr(name,recipe[i].name) != NULL){//??¬?????????????????????????????????
			////printf("|%s|\n",recipe[i].name);
			for(int j=0;j<recipe[i].cnt;j++){
				money = money + keisan(recipe[i].zairyo[j]/*,money*/);
				//printf("%d Money=%d\n",j,money);
			}
			return money;
		}
	}
	for(int i=0;i<n;i++){
		if(strstr(name,IP[i].na) != NULL){
			////printf("%s %d\n",IP[i].na,IP[i].cost);
			return IP[i].cost;
		}
	}
}

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

using namespace std;

map<string, int> item;
map<string, vector<string>> recipe;

int min_score(string tage) {
	if (recipe[tage].size() == 0)return item[tage];

	int sum = 0;
	for (int i = 0; i < recipe[tage].size(); i++) sum += min_score(recipe[tage][i]);

	return min(sum, item[tage]);
}

int main() {
	int n;
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			string tmp; cin >> tmp;
			cin >> item[tmp];
		}
		int recipe_n; cin >> recipe_n;
		for (int i = 0; i < recipe_n; i++) {
			string targetItem; int targetItemRecipe;
			cin >> targetItem >> targetItemRecipe;
			for (int i = 0; i < targetItemRecipe; i++) {
				string tmp;
				cin >> tmp;
				recipe[targetItem].push_back(tmp);
			}
		}

		string tage;
		cin >> tage;
		cout << min_score(tage) << endl;

		item.clear();
		recipe.clear();
	}
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
map<string,int> data;
map<string,vector<string> >ren;

int res(string s){
	if(!ren.count(s))return data[s];
	int sum = 0;
	for(int i =0;i <ren[s].size();i++){
		sum +=res(ren[s][i]);
	}
	if(data[s]>sum){
		data[s]=sum;
	}
	return data[s];
}

int main(){
	int n,m;
	while(cin>>n,n){
		data.clear();
		ren.clear();
		for(int i =0;i < n;i++){
			string item;
			int cst;
			cin>>item>>cst;
			data[item] =cst;
		}
		cin>>m;
		int k;
		for(int i =0;i <m;i++){
			string renkin;
			cin>>renkin>>k;
			//int sum=0;
			for(int j =0;j <k;j++){
				string tmp;
				cin>>tmp;
				ren[renkin].push_back(tmp);
			}
			//data[renkin]=min(sum,data[renkin]);
		}
		string ans;
		cin>>ans;
		cout <<res(ans)<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#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;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
struct item
{
	string name;
	int cost,size;
	vector<string> le;
};
item lis[100];
int cos[100];
int msearch(string s)
{
	for(int i=0;i<n;i++)
	{
		if(lis[i].name==s)return i;
	}
}
int getcost(int v)
{
	if(cos[v]!=-1)return cos[v];
	else
	{
		if(lis[v].size==0)return lis[v].cost;
		else
		{
			int ans=0;
			for(int i=0;i<lis[v].size;i++)
			{
				ans+=getcost(msearch(lis[v].le[i]));
			}
			cos[v]=ans;
			return ans;
		}
	}
}
int main()
{
	while(1)
	{
		fill(cos,cos+100,-1);
		cin >> n;
		if(n==0)break;
                string s;
		int c;
		for(int i=0;i<n;i++)
		{
			cin >> s >> c;
			lis[i].name=s;
			lis[i].cost=c;
			lis[i].size=0;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			int t,k;
			cin >> s;
			cin >> t;
			for(int j=0;j<t;j++)
			{
				string st;
				cin >> st;
				k=msearch(s);
				lis[k].le.pb(st);
			}
			lis[k].size=t;
		}
		string str;
		cin >> str;
		cout << getcost(msearch(str))<< endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
map<string, int> id;
int A[100], memo[100];
vector<int> G[100];

int dfs(int x) {
  if (memo[x] != -1) return memo[x];
  int m = A[x];
  if (G[x].size()) {
    int s = 0;
    for (int t : G[x]) s += dfs(t);
    m = min(m, s);
  }
  return memo[x] = m;
}

int read() {
  string s;
  cin >> s;
  assert(id.find(s) != id.end());
  return id[s];
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> N) {
    if (N == 0) break;
    id.clear();
    rep(i, N) G[i].clear();
    rep(i, N) {
      string s;
      cin >> s >> A[i];
      id[s] = i;
    }
    cin >> M;
    rep(_, M) {
      int s = read();
      int k;
      cin >> k;
      rep(i, k) G[s].pb(read());
    }
    rep(i, N) memo[i] = -1;
    int o = read();
    cout << dfs(o) << "\n";
  }
  return 0;
}