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>
using namespace std;

int n;
string name[100];
int price[100];
int m;
string o[100];
int k[100];
string q[100][100];

int minprice(string item){
  int i,j;
  int tmp,min,flag;;

  for(i=0;i<n;i++){
    if(name[i] == item)min = price[i];
  }

  tmp = 0;
  flag = 0;
  for(i=0;i<m;i++){
    if(o[i] == item){
      flag = 1;
      for(j=0;j<k[i];j++){
	tmp += minprice(q[i][j]);
      }
    }
  }
  
  if(flag && min>tmp)return tmp;
  else return min;
}

int main(){
  int i,j;
  string item;

  while(1){
    cin >> n;
    if(!n)break;

    for(i=0;i<n;i++)cin >> name[i] >> price[i];

    cin >> m;
    for(i=0;i<m;i++){
      cin >> o[i] >> k[i];
      for(j=0;j<k[i];j++)cin >> q[i][j];
    }
    cin >> item;

    cout << minprice(item) <<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, m;
string buf;
map<string, int> cost, memo;
map<string, vector<string>> mat;

int dfs(string s) {
  if (memo.find(s) != memo.end()) return memo[s];
  if (mat.find(s) == mat.end()) return memo[s] = cost[s];
  int res = 0;
  for (string t : mat[s]) {
    res += dfs(t);
  }
  return memo[s] = min(cost[s], res);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  while (cin >> n, n) {
    memo.clear();
    cost.clear();
    mat.clear();
    for (int i = 0; i < n; i++) {
      string s;
      int p;
      cin >> s >> p;
      cost[s] = p;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      string o;
      int k;
      cin >> o >> k;
      for (int i = 0; i < k; i++) {
        string q;
        cin >> q;
        mat[o].emplace_back(q);
      }
    }
    string t;
    cin >> t;
    cout << dfs(t) << endl;
  }

  return 0;
}

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

using namespace std;

class Use_Item{
	public:
		
		void Set_Item(string s, int num){		//zñÌY¦Æ¶ñðnµÄ °Ä­¾³¢
			Item_List[num] = s;
		}
		int cont;
		string Item_List[100];
};

int n,m,t;

//i¨Xg
vector<pair<string,int> > item;

//ÅIIÉìéACe
string make_item;

//ÀÛÌVs
Use_Item ui[100];

//VsÉ éACeXg
string list[100];

int saiki(string make_item)
{
	int total = 0;
	int kane = 0;
	bool find = false;
	
	//ACeðT·
	for(int i = 0; i < n; i++){
		if(item[i].first == make_item){
			kane = item[i].second;
		}
	}
//	cout << make_item << "ðTõ" << endl;
	//VsðTµÄìéACeª é©Ç¤©ðõ
	for(int i = 0; i < m; i++){
		if(list[i] == make_item){
			find = true;
			//©Â©Á½çAVs©çACeXgÌACeðnµÄÄA
			for(int j = 0; j < m; j++){
				total += saiki(ui[i].Item_List[j]);
			}
			return min(kane, total);
		}
	}
	
	if(find == false){
		return kane;
	}
}

int main()
{
	for(;;){
		string temp, temp2;
		
		//ACeÌÍn
		cin >> n;
		if(n == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> temp;
			cin >> t;
			item.push_back(make_pair(temp,t));
		}
		
		//VsÌÍm
		cin >> m;
		
		for(int i = 0; i < m; i++){
			cin >> temp;
			cin >> t;
			
			list[i] = temp;
			ui[i].cont = t;
			for(int j = 0; j < t; j++){
				cin >> temp2;
				ui[i].Set_Item(temp2,j);
			}
		}
		cin >> make_item;
		
		
		//±±ÜÅCvbg
		
		cout << saiki(make_item) << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
	while (cin >> N, N) {
		REP(i, N) cin >> S[i] >> P[i];
		cin >> M;
		REP(i, M) {
			cin >> O[i] >> K[i];
			REP(j, K[i]) cin >> Q[i][j];
		}
		cin >> T;
		
		map<string, int> conv;
		REP(i, N) conv[S[i]] = conv.size() - 1;
		
		REP(i, M) g[i].clear();
		REP(i, M) {
			g[i].push_back(conv[O[i]]);
			REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
		}
		
		fill(cost, cost + 100, INF);
		REP(i, N) chmin(cost[conv[S[i]]], P[i]);
		
		REP(t, M - 1) {
			REP(i, M) {
				ll sum = 0;
				FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
				chmin(cost[g[i][0]], sum);
			}
		}
		
		cout << cost[conv[T]] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main(){
	int n;
	while(cin>>n,n){
		map<string,int>in;
		rep(i,n){
			string s;int a;
			cin>>s>>a;
			in[s]=a;
		}
		cin>>n;
		rep(i,n){
			string s;
			int a;
			cin>>s>>a;
			int sum=0;
			rep(j,a){
				string tmp;
				cin>>tmp;
				sum+=in[tmp];
			}
			in[s]=min(in[s],sum);
		}
		string s;cin>>s;
		cout<<in[s]<<endl;
	}
	
}

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

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;
}