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

using namespace std;

#define INF 1 << 10

int main() {
	int n;

	while (cin >> n, n++) {
		map<string, int> items;
		int prices[101];
		fill(&prices[0], &prices[0]+101, INF);

		for (int i = 1; i < n; ++i) {
			string name; cin >> name;
			int price; cin >> price;
			items[name] = i;
			prices[i] = price;
		}

		int m; cin >> m;
		vector< vector< vector<int> > > recipes(n);
		for (int i = 0; i < m; ++i) {
			string o; cin >> o;
			int k; cin >> k;

			vector<int> v;
			for (int j = 0; j < k; ++j) {
				string name; cin >> name;
				v.push_back(items[name]);
			}
			recipes[items[o]].push_back(v);
		}

		while (1) {
			bool update = false;

			for (int i = 1; i < n; ++i) {
				for (int j = 0; j < recipes[i].size(); ++j) {
					int price = 0;
					for (int k = 0; k < recipes[i][j].size(); ++k)
						price += prices[recipes[i][j][k]];

					if (price < prices[i]) prices[i] = price, update = true;
				}
			}

			if (!update) break;
		}

		string query; cin >> query;
		cout << prices[items[query]] << endl;
	}
}