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 <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int n;
int price[100];
vi recipe[100];
int memo[100];
int search(int id)
{
	if(memo[id] >= 0)
		return memo[id];
	if(memo[id] == -2)
		return price[id];
	if(recipe[id].empty())
		return memo[id] = price[id];
	
	memo[id] = -2;
	int sum = 0;
	rep(i, recipe[id].size()){
		sum += search(recipe[id][i]);
	}
	return memo[id] = min(price[id], sum);
}

int main()
{
	int n;
	while(scanf("%d", &n), n){
		fill(memo, memo+n, -1);
		rep(i, n)
			recipe[i].clear();
		
		map<string, int> dict;
		rep(i, n){
			char tmp[128];
			scanf("%s%d", tmp, &price[i]);
			dict[tmp] = i;
		}
		
		int m;
		scanf("%d", &m);
		rep(i, m){
			char tmp[128];
			scanf("%s", tmp);
			int id = dict[tmp];
			int k;
			scanf("%d", &k);
			rep(j, k){
				scanf("%s", tmp);
				recipe[id].push_back(dict[tmp]);
			}
		}
		
		char tmp[128];
		scanf("%s", tmp);
		printf("%d\n", search(dict[tmp]));
	}
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

        map<string, int> item;
        for(int i=0; i<n; ++i){
            string name;
            int cost;
            cin >> name >> cost;
            item[name] = cost;
        }

        int m;
        cin >> m;
        vector<vector<string> > recipe(m);
        for(int i=0; i<m; ++i){
            string s;
            int k;
            cin >> s >> k;
            recipe[i].resize(k+1);
            recipe[i][0] = s;
            for(int j=1; j<=k; ++j)
                cin >> recipe[i][j];
        }

        for(int i=0; i<m; ++i){
            for(int j=0; j<m; ++j){
                int cost = 0;
                for(unsigned k=1; k<recipe[j].size(); ++k)
                    cost += item[recipe[j][k]];
                item[recipe[j][0]] = min(item[recipe[j][0]], cost);
            }
        }

        string s;
        cin >> s;
        cout << item[s] << endl;
    }
}

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

struct item{
	int p;
	int memo;
	vector<item*> q;
	
	item(): memo(-1) {}
};

map<string,item> table;

int getcost(item &it){
	if(it.memo == -1){
		it.memo = it.p;
		if(!it.q.empty()){
			int s = 0;
			for(int i = 0; i < it.q.size(); ++i){
				s += getcost(*it.q[i]);
			}
			it.memo = min(it.p, s);
		}
	}
	return it.memo;
}

int main(){
	int n, m, p, k;
	string s;
	while(cin >> n, n){
		table.clear();
		for(int i = 0; i < n; ++i){
			cin >> s >> p;
			table[s].p = p;
		}
		cin >> m;
		for(int i = 0; i < m; ++i){
			cin >> s >> k;
			vector<item*> &v = table[s].q;
			v.resize(k);
			for(int j = 0; j < k; ++j){
				cin >> s;
				v[j] = &table[s];
			}
		}

		cin >> s;
		cout << getcost(table[s]) << '\n';
	}
}

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

using namespace std;

struct Item {
	string name;
	int value;
};

Item item[100];

int newValue[100];

struct Recipe {
	int item;
	int len;
	int suppli[100];
};

Recipe recipe[100];


int main () {

	while(true) {
		for ( int i=0; i<100; i++ ) {
			item[i].value = -1;

			for ( int j=0; j<100; j++ ) {
				recipe[i].suppli[j] = 0;
			}
		}


		int itemNum;
		cin >> itemNum;

		if ( itemNum == 0 )
			break;

		for ( int i=0; i<itemNum; i++ ) {
			cin >> item[i].name;
			cin >> item[i].value;
			//cout << item[i].name << item[i].value << endl;
		}
		
		int recipeNum;
		cin >> recipeNum;
		for ( int i=0; i<recipeNum; i++ ) {
			string find;
			cin >> find;
			for ( int j=0; j<itemNum; j++ ) {
				if ( find.compare(item[j].name)==0 ) {
					recipe[i].item = j;
					break;
				}
			}

			cin >> recipe[i].len;
			//			cout << "len: " << recipe[i].len << endl;

			for ( int j=0; j<recipe[i].len; j++ ) {
				cin >> find;
				//				cout << "Find: " << find << " ";
				for ( int k=0; k<itemNum; k++ ) {
					//cout << "Now: " << item[k].name << endl;
					
					if (find.compare(item[k].name)==0) {
						//cout << "Found: " << item[j].name << endl << endl;
						recipe[i].suppli[j] = k;
						break;
					}
				}
			}
		}



		for ( int i=0; i<recipeNum; i++ ) {
			int sum = 0;
			for ( int j=0; j<recipe[i].len; j++ ) {
				sum += item[recipe[i].suppli[j]].value;
			}
			if ( item[recipe[i].item].value > sum )
				item[recipe[i].item].value = sum;
		}

		//cout << endl;
		for ( int i=0; i<itemNum; i++ ) {
			//cout << item[i].name << item[i].value << endl;
		}
			
		string find;
		cin >> find;

		for ( int i=0; i<itemNum; i++ ) {
			if ( find.compare(item[i].name)==0 ) {
				cout << item[i].value << endl;
				break;
			}
		}
	}
}

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

map<string,int> price;
map<string,int> id;
map<int,string> di;
bool table[100][100];

int calcPrice(string t, int n) {

    bool flag = false;
    int tmp = 0;
    for(int i = 0; i < n; i++) {
       if(table[id[t]][i]) {
           flag = true;
           tmp += calcPrice(di[i], n);
       }
    }
    if(!flag) return price[t];

    if(tmp > price[t]) return price[t];

    return tmp;
}


int main(void){
    // Your code here!
    int n;
    while(true) {
        cin >> n;
        if(n==0) break;

        price.clear();
        id.clear();
        di.clear();
        
        string s;
        int p;
        for(int i = 0; i < n; i++) {
            cin >> s >> p;
            price[s] = p;
            id[s] = i;
            di[i] = s;
        }

        for(int i = 0; i < 100; i++) {
            for(int j = 0; j < 100; j++) {
                table[i][j] = false;
            }
        }
        
        int m;
        cin >> m;
        for(int i = 0; i < m; i++) {
            int k;
            string o;
            cin >> o >> k;
            string q;
            for(int j = 0; j < k; j++) {
                cin >> q;
                table[id[o]][id[q]] = true;
            }
        }
        string t;
        cin >> t;
        cout << calcPrice(t, n) << endl;
    }
}