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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
struct item
{
	string name;
	int cost,size;
	vector<string> le;
};
item lis[100];
int memo[100];
int mysearch(string s)
{
	for(int i=0;i<n;i++)
	{
		if(lis[i].name==s)return i;
	}
}
int getcost(int v)
{
	if(memo[v]!=-1)return memo[v];
	else
	{
		if(lis[v].size==0)return lis[v].cost;
		else
		{
			int ans=0;
			for(int i=0;i<lis[v].size;i++)
			{
				ans+=getcost(mysearch(lis[v].le[i]));
			}
			memo[v]= min(ans,lis[v].cost);
			list[v].cost=memo[v];
			return memo[v];
		}
	}
}
int main()
{
	while(1)
	{
		fill(memo,memo+100,-1);
		cin >> n;
		if(n==0)break;
		string s;
		int c;
		for(int i=0;i<n;i++)
		{
			cin >> s >> c;
			lis[i].name=s;
			lis[i].cost=c;
			lis[i].size=0;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			int t,k;
			cin >> s;
			cin >> t;
			for(int j=0;j<t;j++)
			{
				string st;
				cin >> st;
				k=mysearch(s);
				lis[k].le.pb(st);
			}
			lis[k].size=t;
		}
		string str;
		cin >> str;
		cout << getcost(mysearch(str))<< endl;
	}
	return 0;
}

C++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cctype>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <set>
#include <complex>
#include <utility>
#include <numeric>
using namespace std;

typedef complex<int> P;

int b[19][19];

int main(){
	
	int n,m;
	map<string,int> a;
	while(cin>>n,n!=0){
	string s;
	int p;
	for(int i=0;i<n;i++){
		cin>>s>>p;
		a[s]=p;
	}
	string o;
	int k;
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>o>>k;
		string q;
		long long temp=0;
		for(int j=0;j<k;j++){
			cin>>q;
			temp+=a[q];
		}
		if(a[o]>temp)
			a[o]=temp;
	}
	string t;
	cin>>t;
	cout<<a[t]<<endl;
	}
	
}

C++: #include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
using namespace std;
typedef pair<int ,int > P;
typedef long long ll;

int N;
map<string,int> data;
int sum(int p) {
    int sum = 0;
    rep(i,p) {
        string s;
        cin>>s;
        sum += data[s];
    }
    return sum;

}
void solve() {
    data.clear();
    string s;
    rep(i,N) {
        int a;
        cin>>s>>a;
        data[s]=a;
    }
    int M;
    cin>>M;
    rep(i,M) {
        int p;
        cin>>s>>p;
        data[s] = min(data[s],sum(p));
    }

    cin>>s;
    cout<<data[s]<<endl;


}
int main() {
    while(cin>>N) {
        if(!N) break;
        solve();

    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;
#define MAX_V 100
#define INF (1<<24)
int V,E,a;
vector<int> G[MAX_V];
vector<int> g[MAX_V];
map<string,int> change;
string str;
int t[MAX_V];

void dijkstra(){
  priority_queue< P , vector<P> , greater<P> > Q;
  for(int i=0;i<V;i++)Q.push(P(t[i],i));
  while(!Q.empty()){
    P p=Q.top();Q.pop();
    int pos=p.second,cost=p.first;
    if(cost>t[pos])continue;
    for(int i=0;i<(int)g[pos].size();i++){
      int w=g[pos][i];
      int sum=0;
      for(int j=0;j<(int)G[w].size();j++)sum+=t[G[w][j]];
      if(sum<t[w]){
	t[w]=sum;
	Q.push(P(t[w],w));
      }
    }
  }
}

int main(){
  while(cin>>V&&V){
    for(int i=0;i<V;i++){
      cin>>str>>t[i];
      G[i].clear();
      g[i].clear();
      change[str]=i;
    }
    cin>>E;
    for(int i=0;i<E;i++){
      cin>>str;
      int v=change[str];
      cin>>a;
      for(int j=0;j<a;j++){
	cin>>str;
	G[v].push_back(change[str]);
	g[change[str]].push_back(v);
      }
    }
    dijkstra();
    cin>>str;
    cout<<t[change[str]]<<endl;
  }
  return 0;
}

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

struct ITEM {
	string name;
	int cost;
	int recipeNum;
	int recipe[100];
};

int itemNum;
ITEM item[100];

int usedNum;
int used[100];

int renkin(int itemId);
int searchId(string name);

int main(){
	int i,j,n;
	string name;

	while(cin>>itemNum,itemNum){
		//item
		for(i=0;i<itemNum;i++) cin>>item[i].name>>item[i].cost;

		//recipe
		cin>>n;
		while(n--){
			cin>>name;i=searchId(name);
			cin>>item[i].recipeNum;
			for(j=0;j<item[i].recipeNum;j++){
				cin>>name;
				item[i].recipe[j]=searchId(name);
			}
		}

		cin>>name;usedNum=0;
		cout<<renkin(searchId(name))<<endl;
	}

	return 0;
}

int renkin(int itemId){
	int work,result=0;
	int i,j;
	
	used[usedNum++]=itemId;
	for(i=0;i<item[itemId].recipeNum;i++){
		j=0;while(j<usedNum && used[j]!=item[itemId].recipe[i]) j++;
		if(j==usedNum){
			work=renkin(item[itemId].recipe[i]);
			usedNum--;
		}else{
			work=-1;
		}
		
		result+=(work!=-1)?work:item[itemId].cost;
	}
	if(i==0 || item[itemId].cost<result) result=item[itemId].cost;

	return result;
}

int searchId(string name){
	int i=0;while(name!=item[i].name) i++;
	return i;
}