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<stdio.h>
#include<string.h>
#include<math.h>
int a,s[101],d,f[101],g=10000001,h[101],q,w,e;
char z[101][101],x[101][101],c[101][101][101],v[101];
int main(void)
{
	int i,j,k;
	while(1){
		scanf("%d",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			scanf("%s %d",z[i],&s[i]);
		}
		for(i=0;i<a-1;i++){
			for(j=i+1;j<a;j++){
				if(strcmp(z[i],z[j])==0){
					if(s[i]<s[j]) s[j]=s[i];
					else s[i]=s[j];
				}
			}
		}
		scanf("%lld",&d);
		for(i=0;i<d;i++){
			scanf("%s %lld",x[i],&f[i]);
			for(j=0;j<f[i];j++){
				scanf("%s",c[i][j]);
			}
			h[i]=0;
		}
		scanf("%s",v);
		for(i=0;i<a;i++) if(strcmp(v,z[i])==0) if(g>s[i]) g=s[i];
		for(i=0;i<d;i++){
			for(j=0;j<d;j++){
				for(k=0;k<f[j];k++){
					if(strcmp(x[i],c[j][k])==0) h[j]++;
				}
			}
		}
		if(d!=0){
			w=0;
			while(1){
				for(i=0;i<d;i++){
					q=0;
					if(h[i]==0){
						w++;
						e=0;
						for(j=0;j<a;j++){
							for(k=0;k<f[i];k++){
								if(strcmp(z[j],c[i][k])==0){
									//printf("\n%s %s\n",z[j],c[i][k]);
									q+=s[j];
									e++;
									break;
								}
							}
						}
						//printf("%d %d\n",e,q);
						if(e==f[i]){
							for(j=0;j<a;j++){
								if(strcmp(z[j],x[i])==0){
									if(s[j]>q){
										s[j]=q;
										q=0;
										break;
									}
									q=0;
									break;
								}
							}
						}
						else q=0;
						if(q!=0){
							s[a]=q;
							strcpy(z[a],x[i]);
							a++;
						}
					}
					h[i]--;
				}
				if(w==d) break;
			}
		}
		//for(i=0;i<a;i++) printf("%s %d\n",z[i],s[i]);
		for(i=0;i<a;i++){
			if(strcmp(v,z[i])==0){
				printf("%lld\n",s[i]);
				break;
			}
		}
	}
	return 0;
}

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);
			lis[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++: #include <bits/stdc++.h>
using namespace std;

map<string, int> v;
map<string, vector<string> > l;

int main()
{
	int n;
	while (scanf("%d", &n), n){
		for (int i = 0; i < n; i++){
			string str;
			int p;
			cin >> str >> p;
			v[str] = p;
		}
		
		int m;
		scanf("%d", &m);
		for (int i = 0; i < m; i++){
			string o;
			int k;
			cin >> o >> k;
			for (int j = 0; j < k; j++){
				string q;
				cin >> q;
				l[o].push_back(q);
			}
		}
		
		for (int k = 0; k < 10000; k++){
		//while (true){
			bool update = false;
			
			typedef map<string, vector<string> >::iterator ite;
			for (ite i = l.begin(); i != l.end(); ++i){
				int sum = 0;
				for (int j = 0; j < (*i).second.size(); j++){
					sum += v[(*i).second[j]];
				}
				if (v[(*i).first] > sum){
					v[(*i).first] = sum;
					update = true;
				}
			}
			
		//	if (!update) break;
		}
		
		string t;
		cin >> t;
		cout << v[t] << endl;
	}
	
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
int n,m,q[100];
char s[100][100],p[100],d[100];
int f(char *str){
	int i;
	for(i=0;i<n&&strcmp(str,s[i]);i++);
	return i;
}
int main()
{
	int i,k,a,b,j;
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(i=0;i<n;i++){
			scanf("%s %d",s[i],&q[i]);
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			b=0;
			scanf("%s %d",p,&k);
			for(j=0;j<k;j++){
				scanf("%s",d);
				b+=q[f(d)];
			}
			q[f(p)]=q[f(p)]>b?b:q[f(p)];
		}
		scanf("%s",p);
		printf("%d\n",q[f(p)]);
	}
	return 0;
}

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

C++: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define SIZE 102

using namespace std;

void Inputdate(void);             //データ入力
void Solve(void);                 //データ処理と出力

int  n,m;
int  sum;                         //合計値
int  Price[SIZE];                 //素材の値段  
int  Quantity[SIZE];              //練金時に必要な素材の種類の数        
char Item[SIZE][SIZE];            //素材の名前
char Recipe[SIZE][SIZE];          //練金の結果
char Material[SIZE][SIZE][SIZE];  //練金に必要な素材
char Favorite[SIZE];              //最終練金結果

int main(){
  
  int  i,j;
  
  while(1){
   
    scanf("%d",&n);

    if(n==0){
      break;
    } 

    /*データの入力*/
    Inputdate();
    
    /*データ処理と出力*/
    Solve();
    
  }

  return 0;
}
void Inputdate(void){

  int  i,j;
  
  /*素材とその値段の入力*/
  
  for(i=0;i<n;i++){
    scanf("%s %d",Item[i],&Price[i]);
  }

  /*練金レシピの入力*/
  
  scanf("%d",&m);  //レシピ数の入力
  
  for(i=0;i<m;i++){
    
    scanf("%s %d",Recipe[i],&Quantity[i]);
    
    for(j=0;j<Quantity[i];j++){
      scanf("%s",Material[i][j]);
    }
    
  }
  
  /*最終練金結果の出力*/
  
  scanf("%s",Favorite);
  
  return;
  
}
void Solve(void){
  
  int i,j,k;
  int Total_Price=0;        //練金にかかった値段
  int Usual_Price=0;        //練金結果の通常価格
  
  /*レシピ数だけ繰り返す*/
  
  for(i=0;i<m;i++){
    
    /*最終でない練金結果の通常価格を探す*/
    
    j=0;
    while(strcmp(Item[j-1],Recipe[i])!=0){
      Usual_Price=Price[j];
      j++;
    }
    
    /*練金にかかる値段を調べる*/
    
    for(j=0;j<Quantity[i];j++){
      for(k=0;k<n;k++){
	if(strcmp(Item[k],Material[i][j])==0){
	  Total_Price+=Price[k];
	  k=n;
	}
      }
    }
    
    /*元値と練金後の価格を比較*/
    
    if(Total_Price<=Usual_Price){
      for(k=0;k<n;k++){
	if(strcmp(Item[k],Recipe[i])==0){
      Price[k]=Total_Price;
      k=n;
      }
    }
    } 
    
    Total_Price=0;
  }

  /*最終練金結果の価格を調べる*/

  for(i=0;i<n;i++){
    if(strcmp(Favorite,Item[i])==0){
      printf("%d\n",Price[i]);
      break;
    }
  }

  return;
}