Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"log"
	"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))
		log.Printf("%+v\n", 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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}
template<typename T,typename U>
ll FindErase(T &v,U tar){
  ll cnt = 0;
  for(auto it = v.begin(); it != v.end();){
    if(*it == tar){
      it = v.erase(it);
      ++cnt;
    }
    else{
      ++it;
    }
  }
  return cnt;
}

template<typename T>
bool SuffixErase(T &v,size_t suf){
  if(suf > v.size()) return false;
  for(auto it = v.begin(); it != v.end();){
    if(distance(v.begin(),it) == suf){
      v.erase(it);
      return true;
    }
    else{
      ++it;
    }
  }
  return false;
}

struct data{
  string name;
  ll n,p;
  bool f;
  vector<string> v;
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while(cin >> n && n){
    string s;
    ll p;
    map<string,ll> m;
    map<string,bool> m2;
    rep(i,n){
      cin >> s >> p;
      m[s] = p;
      m2[s] = true;
    }
    ll t;
    cin >> t;
    vector<data> v(t);
    rep(i,t){
      cin >> v[i].name;
      v[i].f = false;
      m2[v[i].name] = false;
      cin >> v[i].n;
      rep(j,v[i].n){
        cin >> s;
        v[i].v.push_back(s);
      }
    }

    while(1){
      bool f = true;
      rep(i,t){
        if(!v[i].f){
          v[i].p = 0;
          rep(j,v[i].n){
            if(!m2[v[i].v[j]]){
              f = false;
              break;
            }
            v[i].p += m[v[i].v[j]];
            if(v[i].n - 1 == j){
              v[i].f = true;
              m2[v[i].name] = true;
              m[v[i].name] = min(m[v[i].name],v[i].p);
            }
          }
        }
      }
      if(f) break;
    }
    cin >> s;
    cout << m[s] << endl;
  }

  return 0;
}

C++: /*
 * 0201.cc: Wrought Gold Master
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef map<string,int> msi;

/* global variables */

string is[MAX_N];
msi imap;
int ps[MAX_N], cache[MAX_N];
vi chlds[MAX_N];
bool used[MAX_N];

/* subroutines */

int rec(int k) {
  if (cache[k] >= 0) return cache[k];
  if (used[k]) return ps[k];
  if (chlds[k].size() == 0) return (cache[k] = ps[k]);

  used[k] = true;
  int sum = 0;
  for (int i = 0; i < chlds[k].size(); i++)
    sum += rec(chlds[k][i]);
  used[k] = false;

  return (cache[k] = min(sum, ps[k]));
}

/* main */

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0) break;

    imap.clear();
    memset(cache, -1, sizeof(cache));
    memset(used, false, sizeof(used));
    
    for (int i = 0; i < n; i++) {
      cin >> is[i] >> ps[i];
      imap[is[i]] = i;
      chlds[i].clear();
    }

    int m;
    cin >> m;

    for (int i = 0; i < m; i++) {
      string si;
      int ki;
      cin >> si >> ki;
      int x = imap[si];

      for (int j = 0; j < ki; j++) {
	string sj;
	cin >> sj;
	chlds[x].push_back(imap[sj]);
      }
    }

    string gl;
    cin >> gl;

    int ans = rec(imap[gl]);

    //for (int i = 0; i < n; i++) {
    //  cout << is[i];
    //  printf(" %d (%d)\n", cache[i], ps[i]);
    //}
    printf("%d\n", ans);
  }

  return 0;
}

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

using namespace std;

map<string,int> item;

const int INF = 2000000000;

class Recipe{
public:
	string To;
	vector<string> From;
	int GetCost(){
		if(From.size() == 0){
			return INF;
		}
		int c = 0;
		vector<string>::iterator it;
		for(it = From.begin();it<From.end();it++){
			c += item[*it];
		}
		return c;
	}
};


map<string,Recipe> recipe;



int itemn;
int recipen;

int main(){
	int self;
	char str[256];
	while(scanf("%d",&itemn) != EOF){
		//if(itemn == 0){break;}
		//printf("CASE\n");
		item.clear();
		recipe.clear();
		//printf("ITEMS %d:\n",itemn);
		for(int i=0;i<itemn;i++){
			scanf("%s %d",str,&self);
			item[str] = self;
			//printf("%s	%d\n",str,self);
		}
		if(scanf("%d",&recipen) == EOF) break;
	
		for(int i=0;i<recipen;i++){
			Recipe r;
			scanf("%s %d",str,&self);
			r.To = str;
			for(int j=0;j<self;j++){
				scanf("%s",str);
				r.From.push_back(str);
			}
			recipe[r.To] = r;
		}
		scanf("%s",str);
		
		//printf("TARGET:%s\n",str);
		printf("%d\n" , item[str] < recipe[str].GetCost() ? item[str] : recipe[str].GetCost() );
	}
	return 0;
}