#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;
int main() {
	vector<long>ll;
	int n, ex;
	string s;
	while (cin >> n){
		if (n == 0)break;
		map<string, long>l;
		for (int i = 0; i < n; i++){
			cin >> s >> ex;
			l[s] = ex;
		}
		cin >> n;
		for (int i = 0; i < n; i++){
			cin >> s >> ex;
			long sum = 0;
			for (int i = 0; i < ex; i++) {
				string str;
				cin >> str;
				sum += l[str];
			}
			l[s] = min(l[s], sum);
		}
		cin >> s;
		ll.push_back(l[s]);
	}
	for (int i : ll) {
		cout << i << endl;
	}
	char c;
	cin >> c;
	return 0;
}