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 <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<1001;i++){
			visit[i]=0;
			item[i].ren=0;
			item[i].price=0;
		}
	}
}

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

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define mp make_pair

int n, m;

void solve() {
  map<string,int> il;

  rep(i,n) {
    string name; int price; cin>>name>>price;
    il.insert(mp(name, price));
  }
  
  map<string,vector<string> > al;
  cin>>m;
  rep(i,m) {
    string name; int tn; cin>>name>>tn;
    vector<string> t_vec(tn);
    rep(j,tn) {
      cin>>t_vec[j];
    }
    al.insert(mp(name, t_vec));
  }
  
  map<string,int> al_p;
  
  
  string item; cin>>item;
  /*
  {
    cout<<"item list"<<endl;
    map<string,int>::iterator it = il.begin();
    while (it != il.end()) {
      cout<<it->first<<"|"<<it->second<<endl;
      it++;
    }
  }
  {
    cout<<"alchem item list"<<endl;
    map<string,vector<string> >::iterator it = al.begin();
    while (it != al.end()) {
      cout<<it->first<<"|";
      rep(i,it->second.size()) cout<<it->second.at(i)<<" ";
      cout<<endl;
      it++;
    }
  }*/
    
  // BàXg©çBàliXgÉliðo^·é
  while (true) {
    if (al.size() == al_p.size()) break;
    map<string,vector<string> >::iterator it = al.begin();
    while (it != al.end()) {
      if (al_p.count(it->first)) {it++;continue;}
      // efÞÌvÌliðvZ·é
      // tOð§ÄÄ¨«AêÂÅàvZÅ«È¢fÞª¶Ý·éêÍ[vðñ·
      int sum_price = 0;
      bool flag = true;
      rep(i,it->second.size()) {
        // BàfÞÌACeªACeXgAÜ½ÍBàXgÉ é©
        string material = it->second.at(i);
        // BàXgÉ èABàliXgÉ³¢êÍcontinue;
        if (il.count(material) && al.count(material)) {
          if (al_p.count(material)) {
            sum_price += min(il[material], al_p[material]);
          } else {
            flag = false;
          }
        }
        // BàXgÉµ©³¢ê
        else if (!il.count(material) && al.count(material)) {
          if (al_p.count(material)) {
            sum_price += al_p[material];
          } else {
            flag = false;
          }
        }
        // ACeXgÉµ©³¢ê
        else if (il.count(material) && !al.count(material)) {
          sum_price += il[material];
        }
      }
      // SÄÌfÞÌliðvZÅ«½
      if (flag) {
        //cout<<"insert "<<it->first<<endl;
        al_p.insert(mp(it->first, sum_price));
      }
      it++;
    }
  }
  
  /*
  {
    cout<<"alchem item price list"<<endl;
    map<string,int>::iterator it = al_p.begin();
    while(it != al_p.end()) {
      cout<<it->first<<"|"<<it->second<<endl;
      it++;
    }
  }*/
  int res;
  if (il.count(item) && al_p.count(item)) {
    res = min(il[item], al_p[item]);
  } else if (!il.count(item) && al_p.count(item)) {
    res = al_p[item];
  } else {
    res = il[item];
  }
  cout<<res<<endl;
}

int main (int argc, char const* argv[]) {
  while (cin>>n, n) {
    solve();
  }
  return 0;
}

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

using namespace std;

int n, m;
struct Item {
    int id, price;
    string name;
    vector<int> edge;
    Item() {}
};
vector<Item> Graph;

int Rec(int id)
{
    int price = Graph[id].price, cmp = 0;

    for (int i = 0; i < Graph[id].edge.size(); ++i)
        cmp += Rec(Graph[id].edge[i]);

    return (cmp == 0) ? price : min(price, cmp);
}

int main()
{
    while (cin >> n, n) {
        string s, d;
        map<string, int> hash;

        Graph.resize(n);
        for (int i = 0; i < n; ++i) {
            int p;

            cin >> s >> p;
            hash.insert(map<string, int>::value_type(s, i));
            Graph[i].id = i;
            Graph[i].price = p;
            Graph[i].name = s;
        }

        cin >> m;
        for (int i = 0; i < m; ++i) {
            int num;

            cin >> s >> num;
            for (int j = 0; j < num; ++j) {
                cin >> d;
                Graph[hash[s]].edge.push_back(hash[d]);
            }
        }

        cin >> s;
        cout << Rec(hash[s]) << endl;
    }

    return 0;
}