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<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
 
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+9
#define EPS 1e-9
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < (k); i++)
typedef long long ll;

int main(){
    int n;
    while(scanf("%d",&n),n){
        map<string, int> data;
        rep(i,n){
            string str;
            int val;
            cin >> str >> val;
            data[str] = val;
        } 
        int m;
        scanf("%d",&m);
        rep(i,m){
            string o;
            cin >> o;
            int k;
            scanf("%d",&k);
            int sum = 0;
            rep(i,k){
                string from;
                cin >> from;
                sum += data[from];
            }
            data[o] = min(data[o],sum);
        }
        string want;
        cin >> want;
        cout << data[want] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)

map<string,int> dict;
int getId(string s) {
  if (dict.find(s) == dict.end()) {
      int n = dict.size();
      dict.insert(make_pair(s, n));
      return n;
  }
  return dict[s];
}

int price[128];
bool edge[128][128];
int ref[128];

int main() {
  for (;;) {
      dict.clear();
      memset(edge,0,sizeof(edge));
      int N; cin >> N; if (!N) break;
      REP(i, N) {
          string s;
          int p; cin >> s >> p;
          price[getId(s)] = p;
      }
      int M; cin >> M;
      REP(i, M) {
          string goal;
          int K;
          cin >> goal >> K;
          REP(k, K) {
              string elem;
              cin >> elem;
              edge[getId(elem)][getId(goal)] = true;
          }
      }
      memset(ref, 0, sizeof(ref));
      REP(i, N) REP(j, N) {
          if (edge[i][j]) ++ref[j];
      }
      for (;;) {
          bool update = false;
          REP(i, N) if (ref[i] == 0) {
              update = true;
              ref[i] = -1;
          }

          REP(i, N) if (ref[i] != -1) {
              int cnt = 0;
              REP(j, N) if (edge[j][i] && ref[j] == -1) ++cnt;
              if (cnt == ref[i]) {
                  int buy = 0;
                  REP(j, N) if (edge[j][i]) buy += price[j];
                  if (cnt > 0 && buy < price[i])
                      price[i] = buy;
                  ref[i] = 0;
              }
          }
          if (!update) break;
      }

      string goal; cin >> goal;
      cout << price[getId(goal)] << endl;
  }
  return 0;
}

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

#define reep(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

map<string,int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x){

    if(recipe[x].size()==0){
        return price[x];
    }

    int ans=0;
    rep(i,recipe[x].size()){
        ans += solve(recipe[x][i]);
    }
    //printf("%d->%d\n",x,price[x]);
    return min(ans, price[x]);
}

int main(){
    int n;
    while(cin >> n,n){
        rep(i,n){
            string name;
            int p;
            cin >> name >> p;
            name_id[name] = i;
            price[i] = p;
        }

        int m;
        cin >> m;
        rep(i,m){
            string name;
            int num;
            cin >> name >> num;
            int id = name_id[name];
            rep(k,num){
                string r;
                cin >> r;
                recipe[id].push_back(name_id[r]);
            }
        }

        string a;
        cin >> a;
        cout << solve(name_id[a]) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
  int n1,n2,bell;
  long long int tmp,money[1000],hess=0;
  string item[1000],ITEM,aizu,riri;

  while(1){

    cin >> n1;
    if(n1==0){
      break;
    }
    for(int i=0;i<n1;i++){
      cin >> item[i] >> money[i];
      //cout << i << endl;
    }

    /*for(int i=0;i<n1-1;i++){
      for(int j=n1-1;j>i;j--){
	if(money[j]<money[j-1]){
	  tmp=money[j];
	  money[j]=money[j-1];
	  money[j-1]=tmp;
	}
      }
      }*/
    
    cin >> n2;
    for(int i=0;i<n2;i++){
      cin >> ITEM >> bell;
      for(int j=0;j<bell;j++){
	cin >> aizu;
	for(int t=0;t<n1;t++){
	  if(item[t]==aizu){
	    hess+=money[t];
	    //cout << hess << endl;
	  }
	}
      }

      for(int j=0;j<n1;j++){
	if(item[j]==ITEM){
	  //cout << "Ai" << endl;
	  if(money[j]>hess){
	    money[j]=hess;
	  }
	  //cout << money[j] << endl;
	}
      }
      hess=0;
    }

    cin >> riri;
    for(int i=0;i<n1;i++){
      if(riri==item[i]){
	cout << money[i] << endl;
      }
    }
  }
  return (0);
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
long long func(string name,  map<string,int> &list,  map<string,vector<string> > &renkin){
  if(renkin.find(name)==renkin.end()) return list[name];
  long long ret = 0;
  vector<string> mat = renkin[name];
  REP(i,mat.size()){
    ret += min((long long)list[mat[i]], func(mat[i],list,renkin));
  }
  return ret;
}

int main(void){
  int n,m;
  while(cin>>n){
    if(n==0) break;
    map<string,int> list;
    map<string,vector<string> > renkin;
    REP(j,n){
      string i;
      int p;
      cin>>i>>p;
      list[i] = p;
    }
    cin>>m;
    REP(i,m){
      string o;
      int k;
      cin>>o>>k;
      vector<string> q(k);
      REP(j,k) cin>>q[j];
      renkin[o] = q;
    }
    string item;
    cin>>item;
    cout<<func(item,list,renkin)<<endl;
  }
  return 0;
}