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 <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);
int solve(map<string, int> list, map<string, vector<string> > lesp, string make){
	vector<string> item = lesp[make];
	int mini = INF;
	int cost = 0;
	rep(i, item.size()){
		if(!lesp[item[i]].empty()){
			cost += solve(list, lesp, item[i]);
		}else{
			cost += list[item[i]];
		}
	}
	mini = min(mini, cost);
	return mini;
}

int main(void){
	int n, m;
	while(cin >> n, n){
		map<string, int> LIST;
		string MAKE;
		rep(i, n){
			string s;
			int cost;
			cin >> s >> cost;
			LIST[s] = cost;
		}
		cin >> m;
		map<string, vector<string> > LESP;
		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(n == 1){
			cout << LIST[MAKE] << endl;
			continue;
		}
		int ans = solve(LIST, LESP, 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();
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
using namespace std;
typedef struct{
  vector<string> sozai;
  int money;
}BUTU;
pair<string,int> P[1002];
BUTU butu[1024];
int n,p,m,a,b;
string s,ss,mokuhyou;
int tansaku(string now){
  for(int i=0;i<n;i++){
    if(P[i].first==now){
      return P[i].second;
    }
  }
  return -1;
}
int check(string now){
  int banti=tansaku(now);
  int f=butu[banti].sozai.size();
  int cnt=0;
  if(f==0){
    return butu[banti].money;
  }
  else{
    for(int i=0;i<f;i++){
      cnt+=check(butu[banti].sozai[i]);
    }
    return min(cnt,butu[banti].money);
  }
  return -1;
}
main(){
  while(1){
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++){
      cin>>s>>p;
      butu[i].money=p;
      P[i].first=s;
      P[i].second=i;
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>s>>a;
      b=tansaku(s);
      for(int j=0;j<a;j++){
	cin>>ss;
	butu[b].sozai.push_back(ss);
      }
    }
    cin>>mokuhyou;
    cout<<check(mokuhyou)<<endl;
  }
}

C++: #include <stdio.h>
#include <string.h>

int n;

typedef struct{
    char name[256];
    int cost;
    char used[100][256];
    int size;
} ITEM;

ITEM list[100];

int getCost(int vertex)
{
    int i, j;
    int ans;
    
    if (list[vertex].size == 0){
        return (list[vertex].cost);
    }
    else {
        ans = 0;
        for (i = 0; i < list[vertex].size; i++){
            for (j = 0; j < n; j++){
                if (strcmp(list[vertex].used[i], list[j].name) == 0){
                    ans += getCost(j);
                }
            }
        }
        list[vertex].cost = list[vertex].cost > ans ? ans : list[vertex].cost;
    }
    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++){
            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 <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;

const int INF = 1<<21;
map<string, int> value;
map<string, vector<string> > R;

int rec(string s) {
  int res = INF;
  if(value.find(s) != value.end()) res = min(res, value[s]);
  if(R.find(s) != R.end()) {
    int sum = 0;
    for(vector<string>::iterator it = R[s].begin(); it != R[s].end(); it++) {
      sum += rec(*it);
    }
    res = min(res, sum);
  }
  return res;
}

main() {
  int n;
  while((cin >> n) && n) {
    value.clear();
    R.clear();
    while(n--) {
      string s;
      int a;
      cin >> s >> a;
      value[s] = a;
    }
    cin >> n;
    while(n--) {
      string s;
      int m;
      cin >> s >> m;
      while(m--) {
	string t;
	cin >> t;
	R[s].push_back(t);
      }
    }
    string s;
    cin >> s;
    cout << rec(s) << endl;
  }
}

C++: // 0201
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct item{
	string name;
	int cost;
};

void update(vector<item> &I, vector<vector<int> > recipe, int m){
	for(int i=0;i<m;i++){
		for(int j=0;j<recipe.size();j++){
			if(recipe[j].size() != 0){
				int co = 0;
				for(int k=0;k<recipe[j].size();k++)
					co += I[recipe[j][k]].cost;

				if(I[j].cost > co)
					I[j].cost = co;
			}
		}
	}
}

int main(){
	while(1){
		int n, p;
		string str;
		item it;
		vector<item> I;

		cin>>n;
		if(n == 0)
			break;

		for(int i=0;i<n;i++){
			cin>>str>>p;
			it.name = str;
			it.cost = p;
			I.push_back(it);
		}

		int m, k;
		vector<vector<int> > recipe;

		recipe.resize(n);

		cin>>m;
		for(int i=0;i<m;i++){
			int l;
			cin>>str;
			
			for(int j=0;j<n;j++)
				if(str == I[j].name)
					l = j;

			cin>>k;
			for(int j=0;j<k;j++){
				cin>>str;
				for(int o=0;o<n;o++)
					if(str == I[o].name)
						recipe[l].push_back(o);
			}
		}

		update(I, recipe, m);

		cin>>str;
		for(int i=0;i<n;i++)
			if(str == I[i].name)
				cout<<I[i].cost<<endl;
	}
	return 0;
}