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.h>
#include <string>
#include <vector>
#include <algorithm>
int n;

struct item{
    string name[256];
    int cost;
    vector<string> used;
    int size;
} ;

item list[100];
int memo[100];

int getCost(int v)
{
    if(memo[v]!=-1)return memo[v];
    int i, j;
    int ans;
    
    if (list[v].size == 0){
        return (list[v].cost);
    }
    else {
        ans = 0;
        for (i = 0; i < list[v].size; i++){
            for (j = 0; j < n; j++){
                if (list[v].used[i]== list[j].name){
                    ans += getCost(j);
                }
            }
        }
        list[v].cost = min(list[v].cost,ans);
    }
    return (list[vertex].cost);
}

int main(void)
{
    int i, j, k;
    int m, kk;
    char key[256], temp[256];
    int look, sum;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        for(i=0;i<n;i++)memo[i]=-1;
        for (i = 0; i < n; i++){
            scanf("%s%d", list[i].name, &list[i].cost);
            list[i].size = 0;
        }
        
        scanf("%d", &m);
        for (i = 0; i < m; i++){
            scanf("%s", key);
            for (j = 0; j < n; j++){
                if (strcmp(key, list[j].name) == 0){
                    look = j;
                }
            }
            scanf("%d", &kk);
            list[look].size = kk;
            for (j = 0; j < kk; j++){
                scanf("%s", temp);
                for (k = 0; k < n; k++){
                    if (strcmp(temp, list[k].name) == 0){
                        strcpy(list[look].used[j], temp);
                    }
                }
            }
        }
        scanf("%s", key);
        for (j = 0; j < n; j++){
            if (strcmp(key, list[j].name) == 0){
                look = j;
            }
        }
        
        printf("%d\n", getCost(look));
        
    }
    
    return (0);
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
map<string,int> item;
map<string,vector<string> > recipe;
typedef long long ll;

ll recurse(string name){
	long long ans = (1ll<<63);
	if(item.find(name) != item.end()){
		ans = item[name];
	}
	if(recipe.find(name) != recipe.end() ){
		ll t = 0;
		rep(i,recipe[name].size()){
			t += recurse(recipe[name][i]);
		}
		ans = min(t,ans);
	}
	return ans;
}

int main(){
	int n;
	while(cin >> n , n){
		item.clear(),recipe.clear();
		rep(i,n){
			string s;
			int p;
			cin >> s >> p;
			item[s] = p;
		}
		int m;
		cin >> m;
		rep(i,m){
			string get;
			int k;
			cin >> get >> k;
			rep(j,k){
				string need;
				cin >> need;
				recipe[get].push_back(need);
			}
		}
		string wantItem;
		cin >> wantItem;
		cout << recurse(wantItem) << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<string, int> psi;
vector<psi> items;
vector<pair<string, vector<psi> > > recipes;

int search_items(string name) {
  for (int i = 0; i < items.size(); i++) {
    if (name == items[i].first) {
      return items[i].second;
    }
  }
  return 0;
}

int dfs(string name) {
  int index;
  for (index = 0; index < recipes.size(); index++) {
    if (recipes[index].first == name) {
      break;
    }
  }

  int ret = 0;
  if (index == recipes.size()) {
    return search_items(name);
  } else {
    for (int i = 0; i < recipes[index].second.size(); i++) {
      ret += min(search_items(recipes[index].second[i].first), dfs(recipes[index].second[i].first));
    }
  }

  return ret;
}


int main(void) {
  int n, m;
  while (1) {
    cin >> n;
    if (n == 0) break;

    for (int i = 0; i < n; i++) {
      string name;
      int cost;
      cin >> name >> cost;
      items.push_back(psi(name, cost));
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      string name;
      int num;
      vector<psi> tmp;
      cin >> name >> num;
      for (int j = 0; j < num; j++) {
        string s;
        cin >> s;
        for (int k = 0; k < n; k++) {
          if (s == items[k].first) {
            tmp.push_back(items[k]);
            break;
          }
        }
      }
      recipes.push_back(pair<string, vector<psi> >(name, tmp));
    }

    string s;
    cin >> s;
    cout << dfs(s) << endl;
  }

  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 i = 0; i < n; i++){
				if (p[i].first == s){
					 o=p[i].second;
					 break;
				}
			}
			for (int i2 = 0; i2 < chan[i].second.size(); i2++){
				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<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int n;
main()
{
	while(cin>>n,n)
	{
		map<string,int>M;
		map<string,vector<string> >A;
		map<string,int>cnt;
		for(int i=0;i<n;i++)
		{
			string s;int a;cin>>s>>a;M[s]=a;
			cnt[s]=0;
		}
		int m;cin>>m;
		for(int i=0;i<m;i++)
		{
			string s;cin>>s;
			int k;cin>>k;
			for(;k--;)
			{
				string t;cin>>t;
				A[s].push_back(t);
				cnt[s]++;
			}
		}
		vector<pair<int,string> >a;
		for(pair<string,int>it:cnt)
		{
			a.push_back(make_pair(it.second,it.first));
		}
		sort(a.begin(),a.end());
		for(int i=0;i<n;i++)
		{
			string s=a[i].second;
			if(cnt[s]==0)continue;
			int now=0;
			for(int j=0;j<cnt[s];j++)now+=M[A[s][j]];
			M[s]=min(M[s],now);
		}
		string s;cin>>s;
		cout<<M[s]<<endl;
	}
}