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>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
typedef vector<string> S;
int n,m,k;string s;
map<string,int> cost;
map<string,S> recipe;
int dfs(string t){
	int ans=0;
	S vs=recipe[t];
	if(!vs.empty()){
		for(S::iterator it=vs.begin();it<vs.end();it++){ans+=dfs(vs.at(it-vs.begin()));}
		cost[t]=min(cost[t],ans);
		return cost[t];
	}else{
		return cost[t];
	}
	
}
int main()
{
	while(cin>>n&&n){
		cost.clear();recipe.clear();
		for(int i=0;i<n;i++){
			int tmp;
			cin>>s>>tmp;
			cost[s]=tmp;
		}
		cin>>m;
		for(int i=0;i<m;i++){
			S vs;vs.clear();
			string t;cin>>s>>k;
			while(k--){cin>>t;vs.push_back(t);}
			recipe[s]=vs;
		}
		cin>>s;
		cout<<dfs(s)<<endl;
	}
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
//#include <fstream>	// require freopen

using namespace std;

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

	while (cin >> n && n ){
		map <string, int > item;
		for (int i = 0; i < n; ++i ){
			string name; int money;
			cin >> name >> money;
			item[name] = money;
		} // end for
		cin >> m;
		map <string, vector <string> > recipe;
		for (int i = 0; i < m; ++i ){
			string p; int k; vector <string> t;
			cin >> p >> k;
			for (int j = 0; j < k; ++j){
				string q;
				cin >> q; t.push_back(q);
			} /// end for
			recipe[p] = t;
		} // end for
		map <string, int > pre_item;

		do{
			pre_item = item;

			map<string, int>::iterator it = item.begin();
			for (; it != item.end(); ++it ){
				string r = (*it).first;
				if (!recipe[r].empty() ){
					vector <string>::iterator jt = recipe[r].begin();
					int res = 0;
					for (; jt != recipe[r].end(); ++jt ){
						map <string, int >::iterator kt = item.begin();
						bool found = false;
						for (; kt != item.end(); ++kt ){
							if ((*jt) == (*kt).first ){
								res += (*kt).second;
								found = true;
							} // end if
						} // end for
						if (!found ){
							break;
						} // end if
					} // end for
					if (jt == recipe[r].end() ){
						item[r] = res;
					} // end if
				} // end if
			} // end for
		}while (pre_item != item );

		string search;
		cin >> search;
		cout << item[search] << endl;

	} // end loop
		
	return 0;
}

C++: #include <iostream>
#include <unordered_map>
#include <vector>
#include <bitset>
#include <utility>
using namespace std;
#define MP make_pair
#define F first
#define S second
#define PB push_back
const int N=100 + 10;
int main(){
    int n,k,val[N],now,q,temp;
    bitset<N>b;
    bool f,c;
    string s;
    unordered_map<string,int> m;
    vector<vector<int>>graph[N];
    vector<int> emp;
    while(true){
        cin>>n;
        if(n==0)return 0;
        b.reset();
        m.clear();
        for(int i=0;i<n;i++){
            cin>>s>>k;
            m.insert(MP(s,i));
            val[i]=k;
        }
        cin>>q;
        for(int i=0;i<q;i++){
            cin>>s>>k;
            now=m[s];
            b[now]=true;
            graph[now].PB(emp);
            while(k--){
                cin>>s;
                graph[now].back().PB(m[s]);
            }
        }
        while(q){
            for(int i=0;i<n;i++)if(!graph[i].empty()){
                f=true;
                for(int j=0;j<graph[i].size();j++)if(!graph[i][j].empty()){
                    f=false;
                    c=true;
                    temp=0;
                    for(int r=0;r<graph[i][j].size();r++){
                        if(b[graph[i][j][r]]){
                            c=false;
                            break;
                        }
                        else temp+=val[graph[i][j][r]];
                    }
                    if(c){
                        val[i]=min(val[i],temp);
                        graph[i][j].clear();
                        q--;
                    }
                }
                if(f){
                    graph[i].clear();
                    b[i]=false;
                }
            }
        }
        cin>>s;
        cout<<val[m[s]]<<endl;
    }
    
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<utility>
#include<vector>
using namespace std;
typedef pair<string, int>P;

int main() {
	int n;
	while (cin >> n, n) {
		vector<P>v;
		for (int i = 0;i < n;i++) {
			string sa;
			int a;
			cin >> sa >> a;
			v.push_back(P(sa, a));
		}

		int m;
		cin >> m;
		for (int i = 0;i < m;i++) {
			string s;
			cin >> s;
			int a;
			cin >> a;

			int it=-1;
			int mny = 0;
			for (int j = 0;j < a;j++) {
				string sb;
				cin >> sb;
				for (int k = 0;k < n;k++) {
					if (sb == v[k].first) {
						mny += v[k].second;
					}
				}
			}
			for (int j = 0;j < v.size();j++) {
				if (s == v[j].first)it = j;
			}
			if (it == -1)v.push_back(P(s, mny));
			else v[it].second = mny;
		}

		string ans;
		cin >> ans;
		for (int i = 0;i < v.size();i++) {
			if (ans == v[i].first) {
				cout << v[i].second << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<map>
using namespace std;main(){int n,c,m;for(;cin>>n,n;){string s,t;map<string,int>M;for(;n--;)cin>>s>>c,M[s]=c;for(cin>>n;n--;M[s]=min(M[s],c))for(c=0,cin>>s>>m;m--;)cin>>t,c+=M[t];cin>>s,cout<<M[s]<<endl;}}

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

using namespace std;

struct ITEM {
	vector<string> rishipi;//???????????????????????´??????????£??????????
	int s_price;//????£?????????????
};

int n, m;
map<string, ITEM> item;//????£?????????????
map<string, int> memo;//?????????????????????????????¢

int getprice(string &_item) {
	if (memo[_item] != 0)return memo[_item];//??????????????¨?¢????

	int sum = 0;
	if (item[_item].rishipi.size() == 0)return memo[_item] = item[_item].s_price;//????£????
	else {
		int sum = 0;
		for (int i = 0; i < item[_item].rishipi.size(); i++) {
			sum += getprice(item[_item].rishipi[i]);//?????°?????????????????????
		}
		if (sum < item[_item].s_price)return memo[_item] = sum;
		else return memo[_item] = item[_item].s_price;
	}

}

int main(){
	while (true) {
		cin >> n;
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			string tmp;
			int a;
			cin >> tmp >> a;
			item[tmp].s_price = a;
			memo[tmp] = 0;
		}
		cin >> m;
		for (int i = 0; i < m; i++) {
			string t;
			cin >> t;
			int a;
			vector<string> tmp;
			cin >> a;
			tmp.resize(a);
			for (int j = 0; j < a; j++) {
				cin >> tmp[j];
			}
			item[t].rishipi = tmp;
		}
		string ans;
		cin >> ans;
		//??\?????????
		cout << getprice(ans) << endl;
		//????????????
	}
	return 0;
}