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>
long long 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("%lld",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			scanf("%s %lld",z[i],&s[i]);
		}
		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){
			while(1){
				for(i=0;i<d;i++){
					q=0;
					w=0;
					e=0;
					if(h[i]==0){
						for(j=0;j<a;j++){
							for(k=0;k<f[i];k++){
								if(strcmp(z[j],c[i][k])==0){
									q+=s[j];
									break;
								}
							}
						}
						for(j=0;j<a;j++){
							if(strcmp(z[j],x[i])==0){
								if(s[j]>q){
									s[j]=q;
									q=0;
									break;
								}
							}
						}
					}
					h[i]--;
				}
				for(i=0;i<d;i++){
					if(h[i]<0) w++;
				}
				if(w==d) break;
			}
		}
		for(i=0;i<a;i++){
			if(strcmp(v,z[i])==0){
				printf("%lld\n",s[i]);
				break;
			}
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long int64;
#define first fr
#define second sc

typedef pair< int , int > Pi;

vector< int > g[111];
map < string , int > data;
int cost[111];
int n, m;

int solve(int x){
  if(g[x].empty())return cost[x];
  int ret = 0 ;
  for(int i = 0 ; i < g[x].size() ; ++i){
    ret += solve(g[x][i]);
  }
  return min(ret,cost[x]);
}

int main()
{
 
  while(cin >> n && n){
    data.clear();
    rep(i,111)g[i].clear();
    for(int i = 0; i < n; i++){
      string s;
      int foo;
      cin >> s >> foo;
      data[s] = i;
      cost[i] = foo;
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      string s;
      int k;
      cin >> s >> k;
      int idx = data[s];
      for(int j = 0; j < k; j++){
        string hoge;
        cin >> hoge;
        g[idx].push_back(data[hoge]);
      }
    }
    string r;
    cin >> r ;
    cout << solve(data[r]) << endl;
  }
  return 0;

}

C++: #include <map>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, p[109], dp[109]; string str; map<string, int> x; vector<int> G[109];
int solve(int pos) {
	if (dp[pos] != -1) return dp[pos];
	int ret = p[pos], sum = 0;
	for (int i : G[pos]) sum += solve(i);
	if (G[pos].size()) ret = min(ret, sum);
	return dp[pos] = ret;
}
int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> str >> p[i];
			x[str] = i;
		}
		cin >> m;
		fill(G, G + n, vector<int>());
		for (int i = 0; i < m; i++) {
			cin >> str; int z = x[str];
			int e; cin >> e;
			for (int j = 0; j < e; j++) {
				cin >> str;
				G[z].push_back(x[str]);
			}
		}
		cin >> str;
		fill(dp, dp + n, -1);
		cout << solve(x[str]) << endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

struct item
{
	int id;
	int price;
};

struct recipe
{
	int id;
	int size;
	int price;
	int ingre[100];
};

item I[100];
recipe R[100];
string T[100];

int solve(int id, int n, int m);

int main(int argc, char** argv)
{
	
	string name, o, q;
	int n, m, p, k;
	
	while( 1 )
	{
		cin >> n;
		if(n==0) break;
		for(int i=0; i<n; i++)
		{
			cin >> T[i];
			I[i].id = i;
			cin >> I[i].price;
		}
		
		cin >> m;
		for(int i=0; i<m; i++)
		{
			cin >> name;
			for(int j=0; j<n; j++)
			{
				if(T[j]==name)
				{
					R[i].id = j;
				}
			}
			cin >> R[i].size;
			
			R[i].price = 0;
			for(int j=0; j<R[i].size; j++)
			{
				cin >> name;
				for(int k=0; k<n; k++)
				{
					if(T[k]==name)
					{
						R[i].ingre[j] = k;
						R[i].price += I[k].price;
					}
				}
			}
		}
		
		cin >> name;
		for(int i=0; i<n; i++)
		{
			if(name==T[i])
			{
				cout << solve(i, n, m) << endl;
			}
		}
	}
	
	return 0;
}

int solve(int id, int n, int m)
{
	int money = 0;
	int min = I[id].price;
	bool is_proc = false;
	
	for(int j=0; j<m; j++)
	{
		if(R[j].id == id)
		{
			is_proc = true;
			money = 0;
			for(int k=0; k<R[j].size; k++)
			{
				money += solve(R[j].ingre[k], n, m);
			}
			if(money < min) min = money;
			break;
		}
	}
	
	if(is_proc) return min;
	else return I[id].price;
}

C++: #include <stdio.h>
#include <string.h> 

//マクロ
#define MAX 101

//グローバル変数
//プロトタイプ宣言

int main(){
  
  int n,i,a,b,j;
  char name[MAX][MAX]={};
  int kakaku[MAX]={};
  char last[MAX]={};
  
  int m,z[MAX]={},count;
  char resipi[MAX][MAX]={};
  char item[MAX][MAX]={};
  int ans=0;
  
  while(1){
    
    scanf("%d",&n); if(n==0) break;
    for(i=0;i<n;i++) scanf("%s %d",name[i],&kakaku[i]);
    
    //レシピに関する作業
    scanf("%d",&m);
    
    //レシピの数だけ
    for(i=0;i<m;i++){
      scanf("%s %d",resipi[i],&z[i]);

      //必要なアイテムの数だけ
      for(j=0;j<z[i];j++) {
	scanf("%s",item[j]);
	//アイテムを探して加算
	for(a=0;a<n;a++){
	  if(strcmp(name[a],item[j])==0) ans+=kakaku[a];
	}
      }
      
      //レシピを探して価格が安ければ更新
      for(b=0;b<n;b++){
	if(strcmp(name[b],resipi[i])==0){
	  if(kakaku[b]>ans){
	    kakaku[b]=ans;
	    ans=0;
	  }
	}
      }
      ans=0;
    }
    
    /*もう一度探す処理*/
    //レシピの数だけ
    for(i=0;i<m;i++){
      //必要なアイテムの数だけ
      for(j=0;j<z[i];j++) {
	//アイテムを探して加算
	for(a=0;a<n;a++){
	  if(strcmp(name[a],item[j])==0) ans+=kakaku[a];
	}
      }
      //レシピを探して価格が安ければ更新
      for(b=0;b<n;b++){
	if(strcmp(name[b],resipi[i])==0){
	  if(kakaku[b]>ans){
	    kakaku[b]=ans;
	  }
	}
      }
      ans=0;
    }
    
    /*------------*/
    
    
    scanf("%s",last);
    for(i=0;i<n;i++){
      if(strcmp(name[i],last)==0) printf("%d\n",kakaku[i]);
    }
  }
  
  return 0;
}