#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
	while (true) {
		int n;
		cin >> n;
		if (n == 0) break;
		map<string, int> name;
		vector<int> price;
		string t;
		int a;
		for (int i=0; i<n; ++i) {
			cin >> t >> a;
			name[t] = i;
			price.push_back(a);
		}

		int m;
		cin >> m;
		vector<vector<int> > renkin(n);
		for (int i=0; i<m; ++i) {
			vector<int> t;
			string s;
			int a;
			cin >> s >> a;
			for (int j=0; j<a; ++j) {
				string u;
				cin >> u;
				t.push_back(name[u]);
			}

			renkin[name[s]] = t;
		}

		string target;
		cin >> target;

		bool changed = true;
		while (changed) {
			changed = false;
			for (int i=0; i<renkin.size(); ++i) {
				if (renkin[i].empty()) continue;
				int c = 0;
				for (int j=0; j<renkin[i].size(); ++j)
					c += price[renkin[i][j]];
				if (price[i] > c) {
					changed = true;
					price[i] = c;
				}
			}
		}

		cout << price[name[target]] << endl;
	}
	return 0;
}