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 <vector>
#include <map>
using namespace std;

class Item{
public:
    string name;
    int cost;
    vector<string> subItems;
    int calc();
};

const int MAX_N = 100;
Item items[MAX_N + 1];
map<string, int> item_idx;

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        item_idx.clear();
        for (int i = 0; i < n; i++) {
            cin >> items[i].name >> items[i].cost;
            item_idx.insert(make_pair(items[i].name, i));
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++) {
            string name;
            int k;
            cin >> name >> k;
            for (int j = 0; j < k; j++) {
                string sub;
                cin >> sub;
                items[item_idx[name]].subItems.push_back(sub);
            }
        }
        string name;
        cin >> name;
        cout << items[item_idx[name]].calc() << endl;
    }
}

int Item::calc() {
    int result = cost;
    if (subItems.size() > 0) {
        result = 0;
        for (vector<string>::iterator it = subItems.begin(); it != subItems.end(); it++) {
            result += items[item_idx[*it]].calc();
        }
    }
    return result;
}

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;
map<string,int> state;

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

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

    state[name]=2;
    return res;
}

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

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

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

        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;
        cout << dfs(str) << endl;
    }
}

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

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][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[i][j]);
	//アイテムを探して加算
	for(a=0;a<n;a++){
	  if(strcmp(name[a],item[i][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;
	  }
	}
      }
    }
    
    /*もう一度探す処理*/
    //レシピの数だけ
    for(i=0;i<m;i++){
      //必要なアイテムの数だけ
      for(j=0;j<z[i];j++) {
	//アイテムを探して加算
	for(a=0;a<n;a++){
	  if(strcmp(name[a],item[i][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;
}

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

#define reep(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

map<string,int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x){

    int len = recipe[x].size();

    if(len==0){
        return price[x];
    }

    int ans=0;
    rep(i,len){
        ans += solve(recipe[x][i]);
    }
    //printf("%d->%d\n",x,price[x]);
    return min(ans, price[x]);
}

int main(){
    int n;
    while(cin >> n,n){
        name_id.clear();
        rep(i,100)recipe[i].clear();
        rep(i,n){
            string name;
            int p;
            cin >> name >> p;
            name_id[name] = i;
            price[i] = p;
        }

        int m;
        cin >> m;

        rep(i,m){
            string name;
            int num;
            cin >> name >> num;
            int id = name_id[name];
            rep(k,num){
                string r;
                cin >> r;
                recipe[id].push_back(name_id[r]);
            }
        }

        string a;
        cin >> a;
        cout << solve(name_id[a]) << endl;
    }
    return 0;
}