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 <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;

map <string, int> minC;
map <string, vector <string> > rela;

bool last_item (string item )
{
	return (rela.find(item) == rela.end() );
}

void dfs (string item )
{
	if (last_item (item ) ){
		return;
	} // end if

	map <string, vector <string> >::iterator it = rela.find(item);
	vector <string> r = (*it).second;
	vector <string>::iterator jt = r.begin();

	int sum = 0;
	int prev = minC[item];

	for (; jt != r.end(); ++jt ){
		dfs ((*jt) );
		sum += minC[(*jt)];
	} // end for
	if (prev > sum )
		minC[item] = sum;

}

int main()
{
//	cut here before submit 
//	freopen ("testcase.wgm", "r", stdin );
	int n, m;

	while (cin >> n && n ){
		minC.clear();
		rela.clear();
		for (int i = 0; i < n; ++i ){
			string ite = "";
			int cost;
			cin >> ite >> cost;
			minC[ite] = cost;
		} // end for
		cin >> m;
		for (int i = 0; i < m; ++i ){
			string s = "";
			cin >> s;
			int num;
			cin >> num;
			vector <string> t (num );
			for (int j = 0; j < num; ++j ){
				cin >> t[j];
			} // end for
			rela[s] = t;
		} // end for
		string qns;
		cin >> qns;
		
		dfs (qns );

		cout << minC[qns] << endl;
	} // end loop
		
	return 0;
}

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

int main()
{
    std::map<std::string, int> ItemData;
    int Item_num;
    
    while (std::cin >> Item_num) {
        if (Item_num == 0) break;
        
        
        for (int i = 0; i < Item_num; ++i) {
            std::string ItemName;
            int ItemPrice;
            
            std::cin >> ItemName >> ItemPrice;
            ItemData[ItemName] = ItemPrice;
        }
        
        int mix_num;
        
        std::cin >> mix_num;
        
        for (int i = 0; i < mix_num; ++i) {
            std::string ItemName;
            int Material_num;
            
            std::cin >> ItemName >> Material_num;
            
            int temp = 0;
            std::string MaterialName;

            for (int j = 0; j < Material_num; ++j) {
                std::cin >> MaterialName;
                temp += ItemData[MaterialName];
                ItemData[ItemName] = std::min(ItemData[ItemName], temp);
            }
        }
        
        std::string Material;
        
        std::cin >> Material;
        
        std::cout << ItemData[Material] << std::endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FFOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,b) FOR(i,0,b)
#define RREP(i,b) FFOR(i,1,b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut,pr> ppr;
typedef priority_queue<pr,Vpr, greater<pr> > PQ;
const int SIZE=5+3*1e+4;
const ut INF=1<<30;
const ld eps=1e-6;
const LL p=7+1e+9;

int N, M;
map<string, int> items;
map<string, vector<string>> recipe;

int dfs(string s){
	int res = 0;
	if(recipe[s].empty()){
		return items[s];
	}
	else{
		REP(i,recipe[s].size()){
			res += dfs(recipe[s][i]);
		}
	}
	return res;
}

int main(){
	while(cin >> N && N){
		string s;
		int n;
		REP(i,N){
			cin >> s >> n;
			items[s] = n;
		}
		cin >> M;
		string c, q;
		int k;
		REP(i,M){
			cin >> c >> k;
			vector<string> tmp;
			REP(j,k){
				cin >> q;
				tmp.push_back(q);
			}
			recipe[c] = tmp;
		}
		string goal;
		cin >> goal;
		cout << dfs(goal) << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main(){
  int n;
  map<string,int> M,T;
  while( cin >> n,n){
    M.clear();
    string name;
    for(int i = 0 ; i < n ; i++ ){
      int price;
      cin >> name >> price;
      M[name] = price;
    }
    int sumprice = 0;
    string nam;
    cin >> n;
    for(int i = 0 ; i < n ; i++ ){
      int s;
      cin >> nam >> s;
      for(int j = 0 ; j < s ; j++ ){
	cin >> name;
	T[nam] += M[name];
      }
      M[nam] = min(M[nam],T[nam]);
    }
    cin >> name;
    cout << M[name] << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  for (;;) {
    int n, m;
    cin >> n;
    if (!n) return 0;
    map<string, int> ps;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      cin >> ps[s];
    }
    cin >> m;
    map<string, vector<string> > rs;
    for (int i = 0; i < m; i++) {
      string s, q;
      int k;
      cin >> s >> k;
      for (int j = 0; j < k; j++) {
        cin >> q;
        rs[s].push_back(q);
      }
    }
    bool f;
    do {
      f = false;
      for (map<string, int>::iterator it = ps.begin(); it != ps.end(); ++it)
        if (!rs[it->first].empty()) {
          int s = 0;
          for (int j = 0; j < rs[it->first].size(); j++)
            s += ps[rs[it->first][j]];
          if (s < ps[it->first]) {
            f = true;
            ps[it->first] = s;
          }
        }
    } while (f);
    string s;
    cin >> s;
    cout << ps[s] << endl;
  }
}