#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define rep(I,N) for(int I = 0; I < (int)(N); I++)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int solve(map<string, int> list, map<string, vector<string> > lesp, string make){
	vector<string> item = lesp[make];
	ll mini = INF;
	ll cost = 0;
	rep(i, item.size()){
		if(!lesp[item[i]].empty()){
			//cout << "item" << endl;
			cost += solve(list, lesp, item[i]);
		}else{
			cost += list[item[i]];
		}
	}
	mini = min(mini, cost);
	return mini;
}

int main(void){
	int n, m;
	while(cin >> n, n){
		map<string, int> LIST;
		string MAKE;
		rep(i, n){
			string s;
			int cost;
			cin >> s >> cost;
			LIST[s] = cost;
		}
		cin >> m;
		map<string, vector<string> > LESP;
		rep(i, m){
			string s;
			cin >> s;
			int num;
			cin >> num;
			vector<string> v;
			rep(j, num){
				string L;
				cin >> L;
				v.push_back(L);
			}
			LESP[s] = v;
		}
		cin >> MAKE;
		if(m == 0){
			cout << LIST[MAKE] << endl;
			continue;
		}
		ll ans = solve(LIST, LESP, MAKE);
		FIN(ans);

	}


	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}