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

using namespace std;

map<string, vector<string> > recipe;
map<string, int> cost;

int search(string item){
    if(recipe[item].size() == 0) return cost[item];
    else {
        int sum = 0;
        for(int i=0; i<recipe[item].size(); i++){
            sum += search(recipe[item][i]);
        }
        return min(cost[item], sum);
    }
}

int main(){
    int n;
    while(cin >> n, n){
        recipe.clear(); cost.clear();
        for(int i=0; i< n; i++){
            string name; int m;
            cin >> name >> m;
            cost[name] = m;
        }
        cin >> n;
        for(int i=0; i< n; i++){
            string item;
            cin >> item;
            int m;
            cin >> m;
            for(int j=0; j < m; j++){
                string material;
                cin >> material;
                recipe[item].push_back(material);
            }
        }
        string item;
        cin >> item;
        cout << search(item) << endl;
    }
}

C++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int,string> P;

static const double eps = 1e-8;

int trainTime[101][101];
int trainCost[101][101];

int main(){
	int n,m;
	while(~scanf("%d",&n)){
		if(n==0) break;
		map<string,int> items;
		for(int i=0;i<n;i++){
			string name;
			int cost;
			cin >> name >> cost;
			items[name] = cost;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			string name;
			cin >> name;
			int k;
			scanf("%d",&k);

			int sum = 0;
			string elem;
			for(int j=0;j<k;j++){
				string elem;
				cin >> elem;
				sum += items[elem];
			}

			items[name] = min(items[name],sum);
		}

		string target;
		cin >> target;

		cout << items[target] << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<string>
#include <map>
#include<algorithm>
using namespace std;
int search(string, pair<string, int>*, pair<string, vector<string> >*);
int n;
int m,si;
int main()
{
	
	while (true){
		pair<string, int>p[101];
		pair<string, vector<string> >chan[101];
		cin >> n;
		if (n == 0){
			break;
		}
		for (int i = 0; i < n; i++){
			cin >> p[i].first >> p[i].second;
		}

		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> chan[i].first >> si;
			for (int i2 = 0; i2 < si; i2++){
				string s;
				cin >> s;
				chan[i].second.push_back(s);
			}
		}
		string s;
		cin >> s;
		int ans = search(s,p,chan);
		cout << ans << endl;
	}
}
int search(string s, pair<string, int>*p, pair<string, vector<string> >*chan){
	int num = 0;
	bool flag = true;
	for (int i = 0; i < m; i++){
		if (chan[i].first == s){
			flag = false;
			int o;
		for (int i3 = 0; i3 < n; i3++){
				if (p[i3].first == s){
					 o=p[i3].second;
					 break;
				}
			}
			for (int i2 = 0; i2 < chan[i].second.size(); i2++){
		
				num += min(search(chan[i].second[i2],p,chan),o);
			}
		}
	}
	if (flag){
		for (int i = 0; i < n; i++){
			if (p[i].first == s){
				num += p[i].second;
			}
		}
	}
	return num;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

struct Info{
	Info(){
		cost = 0;
	}
	char name[101];
	int cost;
};

struct Recipe{
	Recipe(){
		index = num = 0;
		used = false;
	}

	void setNUM(int arg_num){
		component = new int[arg_num];
	}

	void push(int id){
		component[index++] = id;
	}

	int* component;
	int menu,index;
	int num;
	bool used;
};


bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

void func(int n){

	Info info[n];
	int checkTable[n];
	for(int i = 0; i < n; i++)checkTable[i] = 0; //0:fix 1:not

	for(int i = 0; i < n; i++){
		scanf("%s %d",info[i].name,&info[i].cost);
	}

	int m;
	scanf("%d",&m);
	char tmp[101];
	Recipe recipes[m];

	for(int i = 0; i < m; i++){
		scanf("%s %d",tmp,&recipes[i].num);
		for(int k = 0; k < n; k++){
			if(strCmp(tmp,info[k].name)){
				recipes[i].menu = k;
				break;
			}
		}

		checkTable[recipes[i].menu] = 1;

		recipes[i].setNUM(recipes[i].num);

		for(int k = 0; k < recipes[i].num; k++){
			scanf("%s",tmp);
			for(int p = 0; p < n; p++){
				if(strCmp(tmp,info[p].name)){
					recipes[i].push(p);
					break;
				}
			}
		}
	}

	int count,sum;
	bool FLG;

	while(true){

		count = 0;
		for(int i = 0; i < n; i++){
			if(checkTable[i] == 1){
				count++;
			}
		}
		if(count == 0)break;

		for(int i = 0; i < m; i++){
			if(recipes[i].used == false){
				FLG = true;
				sum = 0;

				for(int k = 0; k < recipes[i].num; k++){
					sum += info[recipes[i].component[k]].cost;
					if(checkTable[recipes[i].component[k]] == 1){
						FLG = false;
						break;
					}
				}
				if(FLG){
					info[recipes[i].menu].cost = min(info[recipes[i].menu].cost,sum);
					recipes[i].used = true;
					checkTable[recipes[i].menu] = 0;
				}
			}
		}
	}

	char to_make[101];
	scanf("%s",to_make);

	for(int i = 0; i < n; i++){
		if(strCmp(info[i].name,to_make)){
			printf("%d\n",info[i].cost);
			break;
		}
	}
}

int main(){

	int n;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		func(n);

	}

	return 0;
}