#include<bits/stdc++.h>

using namespace std;

int N;
map<string, int> BaseList;


void solve() {
	int m, num, sum;
	string name, tmp;

	/*for (map<string, int>::iterator it = BaseList.begin(); it!= BaseList.end(); ++it) {
		cout << it->first << " " << it->second << endl;
	}*/
	cin >> m;
	for (int i = 0; i < m; ++i) {
		cin >> name >> num;
		sum = 0;
		for (int j = 0; j < num; ++j) {
			cin >> tmp;
			sum += BaseList[tmp];
		}
		if (sum < BaseList[name]) {
			BaseList[name] = sum;
		}
	}
	cin >> name;
	cout << BaseList[name] << endl;
}

int main() {
	int value;
	string str;

	while (cin >> N, N) {
		BaseList.clear();
		for (int i = 0; i < N; ++i) {
			cin >> str >> value;
			BaseList[str] = value;
		}
		solve();
	}

	return 0;
}
