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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		map<string, long long int> mp;
		string a;
		long long int b;
		for (int i = 0; i < n; i++) {
			cin >> a >> b;
			mp[a] = b;
		}
		int m;
		cin >> m;
		vector<string> data[105];
		string g;
		for (int i = 0; i < m; i++) {
			cin >> g;
			data[i].push_back(g);
			int w;
			cin >> w;
			for (int j = 0; j < w; j++) {
				cin >> g;
				data[i].push_back(g);
			}
		}
		bool hantei = true;
		while (hantei) {
			hantei = false;
			for (int i = 0; i < m; i++) {
				long long int p = 0;
				for (int j = 1; j < data[i].size(); j++) {
					p += mp[data[i][j]];
				}
				if (mp[data[i][0]] > p) {
					mp[data[i][0]] = p;
					hantei = true;
				}
			}
		}
		string h;
		cin >> h;
		cout << mp[h] << endl;
	}
	return 0;
}