#include<iostream>
#include<vector>
#include<map>
#include<string>

using namespace std;

struct ITEM {
	vector<string> rishipi;//???????????????????????´??????????£??????????
	int s_price;//????£?????????????
};

int n, m;
map<string, ITEM> item;//????£?????????????
map<string, int> memo;//?????????????????????????????¢

int getprice(string &_item) {
	if (memo[_item] != 0)return memo[_item];//??????????????¨?¢????

	int sum = 0;
	if (item[_item].rishipi.size() == 0)return memo[_item] = item[_item].s_price;//????£????
	else {
		int sum = 0;
		for (int i = 0; i < item[_item].rishipi.size(); i++) {
			sum += getprice(item[_item].rishipi[i]);//?????°?????????????????????
		}
		if (sum < item[_item].s_price)return memo[_item] = sum;
		else return memo[_item] = item[_item].s_price;
	}

}

int main(){
	while (true) {
		cin >> n;
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			string tmp;
			int a;
			cin >> tmp >> a;
			item[tmp].s_price = a;
			memo[tmp] = 0;
		}
		cin >> m;
		for (int i = 0; i < m; i++) {
			string t;
			cin >> t;
			int a;
			vector<string> tmp;
			cin >> a;
			tmp.resize(a);
			for (int j = 0; j < a; j++) {
				cin >> tmp[j];
			}
			item[t].rishipi = tmp;
		}
		string ans;
		cin >> ans;
		//??\?????????
		cout << getprice(ans) << endl;
		//????????????
	}
	return 0;
}