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<iostream>
#include<string>
#include<vector>
#include<map>

using namespace std;

class Use_Item{
	public:
		
		void Set_Item(string s, int num){		//zñÌY¦Æ¶ñðnµÄ °Ä­¾³¢
			Item_List[num] = s;
		}
		int cont;
		string Item_List[100];
};

int n,m,t;

//i¨Xg
vector<pair<string,int> > item;

//ÅIIÉìéACe
string make_item;

//ÀÛÌVs
Use_Item ui[100];

//VsÉ éACeXg
string list[100];

int saiki(string make_item)
{
	int total = 0;
	int kane = 0;
	bool find = false;
	
	//ACeðT·
	for(int i = 0; i < n; i++){
		if(item[i].first == make_item){
			kane = item[i].second;
		}
	}
	//VsðTµÄìéACeª é©Ç¤©ðõ
	for(int i = 0; i < m; i++){
		if(list[i] == make_item){
			find = true;
			//©Â©Á½çAVs©çACeXgÌACeðnµÄÄA
			for(int j = 0; j < m; j++){
				total += saiki(ui[i].Item_List[j]);
			}
			return min(kane, total);
		}
	}
	
	if(find == false){
		return kane;
	}
}

int main()
{
	for(;;){
		string temp, temp2;
		fill_n(list, 100, "");
		item.clear();
		
		//ACeÌÍn
		cin >> n;
		if(n == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> temp;
			cin >> t;
			item.push_back(make_pair(temp,t));
		}
		
		//VsÌÍm
		cin >> m;
		
		for(int i = 0; i < m; i++){
			cin >> temp;
			cin >> t;
			
			list[i] = temp;
			ui[i].cont = t;
			for(int j = 0; j < t; j++){
				cin >> temp2;
				ui[i].Set_Item(temp2,j);
			}
		}
		cin >> make_item;
		
		
		//±±ÜÅCvbg
		
		cout << saiki(make_item) << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

map<string,int> table;
map<string,vector<string>> recipe;
map<string,int> opt;

int dfs(string& name,map<string,int>& state){
    state[name]=1;
    int res=table[name];
    if(recipe[name].empty()) return opt[name]=res;

    int tmp=0;
    for(auto &next:recipe[name]){
        if(state[next]==1){
            state[name]=2;
            return opt[name]=res;
        }
        if(state[next]==2) tmp+=opt[next];
        else tmp+=dfs(next,state);
    }
    res=min(res,tmp);

    state[name]=2;
    return opt[name]=res;
}

void solve(){
    int n;
    while(true){
        cin >> n;
        if(!n) break;

        table.clear();
        recipe.clear();
        opt.clear();

        rep(i,0,n){
            string name;
            int cost;
            cin >> name >> cost;
            table[name]=cost;
            opt[name]=inf;
        }

        int m;
        cin >> m;
        rep(i,0,m){
            string name1;
            int k;
            cin >> name1 >> k;
            rep(j,0,k){
                string name2;
                cin >> name2;
                recipe[name1].push_back(name2);
            }
        }

        string str;
        cin >> str;
        map<string,int> state;
        cout << dfs(str,state) << endl;
    }
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
  int n1,n2,bell;
  long long int tmp,money[100],hess=0;
  string item[100],ITEM,aizu,riri;

  while(1){

    cin >> n1;
    if(n1==0){
      break;
    }
    for(int i=0;i<n1;i++){
      cin >> item[i] >> money[i];
      //cout << i << endl;
    }

    /*for(int i=0;i<n1-1;i++){
      for(int j=n1-1;j>i;j--){
	if(money[j]<money[j-1]){
	  tmp=money[j];
	  money[j]=money[j-1];
	  money[j-1]=tmp;
	}
      }
      }*/
    
    cin >> n2;
    for(int i=0;i<n2;i++){
      cin >> ITEM >> bell;
      for(int j=0;j<bell;j++){
	cin >> aizu;
	for(int t=0;t<n1;t++){
	  if(item[t]==aizu){
	    hess+=money[t];
	    cout << hess << endl;
	  }
	}
      }

      for(int j=0;j<n1;j++){
	if(item[j]==ITEM){
	  //cout << "Ai" << endl;
	  if(money[j]>hess){
	    money[j]=hess;
	  }
	  //cout << money[j] << endl;
	}
      }
      hess=0;
    }

    cin >> riri;
    for(int i=0;i<n1;i++){
      if(riri==item[i]){
	cout << money[i] << endl;
      }
    }
  }
  return (0);
}

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

struct data{
	char name[101];		//¼O
	int price;			//li
	int ren;			//BàÅ«é©
	int renkin[101];	//BàÅg¤itemÌzñÌY¦[0]ÉÍÂðüêÄ¨­
};
data item[101];
int visit[101];			//»ÌACeðêxKê½©Ç¤©B

int func(int number){
	int c=item[number].renkin[0];
	int ans=0;
	visit[number]=1;
//	printf("%s\n",item[number].name);
	if(item[number].renkin[0]==0){
		return item[number].price;
	}
	for(int i=1;i<=c;i++){
		if(visit[item[number].renkin[i]]==1){
		//	printf("visited!! %s %d\n",item[item[number].renkin[i]].name,item[item[number].renkin[i]].price);
			ans+=item[item[number].renkin[i]].price;
		}
		else if(item[item[number].renkin[i]].ren==0){
//			printf("Can't!!   %s %d\n",item[item[number].renkin[i]].name,item[item[number].renkin[i]].price);
			ans+=item[item[number].renkin[i]].price;
		}
		else {
//			printf("renkin!!  %s %d\n",item[item[number].renkin[i]].name,item[item[number].renkin[i]].price);
			ans+=func(item[number].renkin[i]);
		}
	}
	if(ans>item[number].price)return item[number].price;
	else {
		item[number].price=ans;
		return ans;
	}
}
			

int main(void){
	int n,i,j,l;
	while(scanf("%d",&n),n){
		
		int m,k,t;
		char temp[101];
		for(i=0;i<n;i++){
			scanf("%s %d",item[i].name,&item[i].price);
		}
		//±±©çBà
		scanf("%d",&m);
		for(i=0;i<m;i++){
			
			//Å«éàÌ
			scanf("%s",temp);
			for(j=0;j<n;j++){
				if(!(strcmp(item[j].name,temp))){
					item[j].ren=1;
					break;
				}
			}
			t=j;
			puts(item[t].name);
			//´¿
			scanf("%d",&k);
			item[t].renkin[0]=k;
			for(j=0;j<k;j++){
				scanf("%s",temp);
				for(l=0;l<n;l++){
					if(!(strcmp(item[l].name,temp))){
						item[t].renkin[j+1]=l;
						break;
					}
				}
			}
		}
		scanf("%s",temp);
		for(i=0;i<n;i++){
			if(!(strcmp(item[i].name,temp))){
				break;
			}
		}
		//±±ÜÅÇÝÝ
		
		
		//wè³ê½ACeÅÄAµÄ¢­
		//©ªªoÄ«½ç¤BBàÅ«È¢B»±ðKê½©Å»fH
	//	puts("ÄA");
		printf("%d\n",func(i));
		
		for(i=0;i<100;i++){
			visit[i]=0;
		}
	}
}