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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n;
	while(cin>>n,n){
		map<string,int> ma;
		vector<pair<string,int>> v(n);
		rep(i,n) cin>>v[i].F>>v[i].S;
		rep(i,n){
			ma[v[i].F]=v[i].S;
		}
		int m;
		cin>>m;
		vector<pair<string,vector<string>>> vp(m);
		rep(i,m){
			cin>>vp[i].F;
			int t;
			cin>>t;
			vector<string> tmp(t);
			rep(j,t){
				cin>>tmp[j];
			}
			vp[i].S=tmp;
		}
		string s;
		cin>>s;
		bool update=false;
		rep(i,m){
			update=false;
			rep(j,m){
				int tmp=0;
				rep(k,vp[j].S.size()){
					tmp+=ma[vp[j].S[k]];
				}
				if(tmp<ma[vp[j].F]){
					ma[vp[j].F]=tmp;
					update=true;
				}
			}
			if(!update) break;
		}
		cout<<ma[s]<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: // 2010/12/22 Tazoe

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

int min_cost(string item, map<string, int>& items, map<string, vector<string>>& recipes)
{
	if(recipes.find(item)==recipes.end())
		return items[item];

	int cost_recipes = 0;
	for(vector<string>::iterator p=recipes[item].begin(); p<recipes[item].end(); p++)
		cost_recipes += min_cost(*p, items, recipes);

	if(cost_recipes<items[item])
		return cost_recipes;
	else
		return items[item];
}

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		map<string, int> items;
		for(int i=0; i<n; i++){
			string i_str;
			int p;
			cin >> i_str >> p;
			items[i_str] = p;
		}

		int m;
		cin >> m;

		map<string, vector<string>> recipes;
		for(int i=0; i<m; i++){
			string o_str;
			int k;
			cin >> o_str >> k;
			for(int j=0; j<k; j++){
				string q_str;
				cin >> q_str;
				recipes[o_str].push_back(q_str);
			}
		}

		string item;
		cin >> item;

		cout << min_cost(item, items, recipes) << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
typedef pair<string,int> P;
map<string,vector<string> > rm;
map<string,int> I;
int N,M;
//map<string,int> dp;
int solve(string S){
  // if(dp[S]!=0) return dp[S]; 
  if(rm[S].empty()) return I[S];
  int n = rm[S].size();
  int ret=0;
  for(int i=0;i<n;i++){
    ret+=solve(rm[S][i]);
  }
  ret = min(I[S],ret);
  return ret;
}

main(){
  while(1){
    cin >> N;
    if(!N) break;
    for(int i=0;i<N;i++){
      string Is;
      int Ii;
      cin >> Is >> Ii;
      I[Is]=Ii;
    }
    cin >> M;
    for(int i=0;i<M;i++){
      string s;
      int k;
      cin >> s >> k;
      for(int i=0;i<k;i++){
	string si;
	cin >> si;
	rm[s].push_back(si);
      }
    }
    string Q;
    cin >> Q;
    cout << solve(Q) << endl;
    rm.clear();
    I.clear();
  }
}

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

using namespace std;

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

int calcMinCost(string item)
{
	if(cost[item]!=-1)	return cost[item];

	vector<string> rcp=recipe[item];
	if(rcp.size()==0)	return cost[item]=price[item];

	int syncost=0;
	for(int i=0;i<rcp.size();i++)	syncost+=calcMinCost(rcp[i]);
	return cost[item]=min(price[item],syncost);
}

int main()
{
	for(int n;cin>>n,n;){
		for(int i=0;i<n;i++){
			string item;
			int p;	cin>>item>>p;
			price[item]=p;
			cost[item]=-1;
		}

		int m;	cin>>m;
		for(int i=0;i<m;i++){
			string item;
			int k;	cin>>item>>k;
			vector<string> material(k);
			for(int j=0;j<k;j++)	cin>>material[j];
			recipe[item]=material;
		}

		string tar;	cin>>tar;
		cout<<calcMinCost(tar)<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

ll n;
map<string, ll> msi;
map<string, vector<string> > msvs;
ll m;
string t;

ll solve(string s){
	if (msvs.find(s) == msvs.end()){
		return msi[s];
	}
	else{
		ll res = 0;
		for (vector<string>::iterator it = msvs[s].begin(); it != msvs[s].end(); it++){
			res += solve(*it);
		}
		return min(msi[s], res);
	}
}

int main(){
	while (cin >> n&&n){
		msi.clear();
		msvs.clear();
		REP(i, n){
			string s;
			ll p;
			cin >> s >> p;
			msi[s] = p;
		}
		cin >> m;
		REP(i, m){
			string o;
			ll k;
			cin >> o >> k;
			REP(j, k){
				string q;
				cin >> q;
				msvs[o].push_back(q);
			}
		}
		cin >> t;
		cout << solve(t) << endl;
	}
}