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

using namespace std;

int main(){
	while(1){
		int n;
		scanf("%d", &n);
		if(n==0) break;
		
		map<string,int> data;
		
		int i;
		char s[100];
		int v;

		for(i=0;i<n;i++){
			scanf(" %s %d", s, &v);
			data.insert(pair<string,int>(string(s),v));
		}
		
		scanf("%d", &n);
		for(i=0;i<n;i++){
			int w;
			scanf(" %s %d", s, &w);
			int v = data[string(s)];
			int nv=0;
			for(int j=0;j<w;j++){
				char ns[100];
				scanf(" %s", ns);
				nv += data[string(ns)];
			}
			if(nv < v) data[string(s)]=nv;
		}
		scanf(" %s", s);
		printf("%d\n", data[string(s)]);
	}
}

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

using namespace std;

int N, M;

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

int rec(string query)
{
  int res = 1000000;
  if (recipe.find(query) != recipe.end()) {
    int tmp = 0;
    for (int i = 0; i < recipe[query].size(); i++) {
      tmp += rec(recipe[query][i]);
    }
    res = min(res, tmp);
  }
  
  res = min(res, mp[query]);
  
  return res;
}

int main()
{
  while (cin >> N && N) {
    mp.clear();
    recipe.clear();
    for (int i = 0; i < N; i++) {
      string key; int val;
      cin >> key >> val;
      mp[key] = val; 
    }
    
    cin >> M;
    for (int i = 0; i < M; i++) {
      int num;
      string key, val;
      vector<string> vs;
      cin >> key >> num;
      for (int j = 0; j < num; j++) {
        cin >> val;
        vs.push_back(val);
      }
      
      recipe[key] = vs;
    }
    
    string q;
    cin >> q;
    cout << min(rec(q), mp[q]) << endl;
  }
}

C++: #include<stdio.h>
#include<string.h>
int n,m,k;
int i,j;
int rec[100][101];
int ck[100];
typedef struct{
	char name[101];
	int price;
}item;
item it[100];
int min(int a,int b){return a<b?a:b;}
int main(){
	while(1){
		scanf("%d",&n);
		if(n==0)return 0;
		for(i=0;i<n;i++)scanf("%s %d",it[i].name,&it[i].price);
		scanf("%d",&m);
		for(i=0;i<m;i++)ck[i]=0;
		for(i=0;i<m;i++){
			char s[101];
			scanf("%s",s);
			for(j=0;j<n;j++)if(strcmp(s,it[j].name)==0){
				rec[i][ck[i]++]=j;
				break;
			}
			scanf("%d",&k);
			for(j=0;j<k;j++){
				scanf("%s",s);
				for(int l=0;l<n;l++)if(strcmp(s,it[l].name)==0){
					rec[i][ck[i]++]=l;
				}
			}
		}
		int res;
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				res=0;
				for(int l=1;l<ck[j];l++)res+=it[rec[j][l]].price;
				it[rec[j][0]].price=min(it[rec[j][0]].price,res);
			}
		}
		char ans[101];
		scanf("%s",ans);
		for(i=0;i<n;i++)if(strcmp(ans,it[i].name)==0)break;
		printf("%d\n",it[i].price);
	}
}

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;
            for (int i2 = 0; i2 < chan[i].second.size(); i2++){
                num += search(chan[i].second[i2],p,chan);
            }break;
        }
    }
    if (!flag){
        int k;
        for (int i = 0; i < n; i++){
            if (p[i].first == s){
                num=min(p[i].second,num);
                break;
            }
        }
    }
    if (flag){
        for (int i = 0; i < n; i++){
            if (p[i].first == s){
                num += p[i].second;
            }
        }
    }
    return num;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
class item {
public:
	string name;
	int price;
	vector<int> recipe;
};
bool compare(item &a, item &b) {
	return a.name < b.name;
}
int find(vector<item> &, string);
int make(vector<item> &, int);
int main() {
	int buy;
	while (cin >> buy&&buy != 0) {
		vector<item> line(buy);
		for (int i = 0; i < buy; ++i) {
			cin >> line[i].name >> line[i].price;
		}
		sort(line.begin(), line.end(), compare);
		int time;
		cin >> time;
		for (int j = 0; j < time; ++j) {
			string d;
			int num;
			cin >> d >> num;
			int place = find(line, d);
			for (int i = 0; i < num; ++i) {
				cin >> d;
				line[place].recipe.push_back(find(line, d));
			}
		}
		string d;
		cin >> d;
		cout << make(line, find(line, d)) << endl;
	}
	return 0;
}

int find(vector<item> &line, string name) {
	int sp = 0;
	int up = line.size();
	while (true) {
		int tmp = (sp + up) / 2;
		if (line[tmp].name==name) {
			return tmp;
		}
		else if (line[tmp].name < name) {
			sp = tmp + 1;
		}
		else {
			up = tmp;
		}
	}
}

int make(vector<item> &line, int place) {
	int res = line[place].price;
	if (!line[place].recipe.empty()) {
		int res2 = 0;
		for (int i = 0, nmax = line[place].recipe.size(); i < nmax; ++i) {
			res2 += make(line, line[place].recipe[i]);
		}
		return min(res, res2);
	}
	return res;
}