#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
class item {
public:
	string name;
	int price;
	vector<int> recipe;
};
bool compare(item &a, item &b) {
	return a.name < b.name;
}
int find(vector<item> &, string);
int make(vector<item> &, int);
int main() {
	int buy;
	while (cin >> buy&&buy != 0) {
		vector<item> line(buy);
		for (int i = 0; i < buy; ++i) {
			cin >> line[i].name >> line[i].price;
		}
		sort(line.begin(), line.end(), compare);
		int time;
		cin >> time;
		for (int j = 0; j < time; ++j) {
			string d;
			int num;
			cin >> d >> num;
			int place = find(line, d);
			for (int i = 0; i < num; ++i) {
				cin >> d;
				line[place].recipe.push_back(find(line, d));
			}
		}
		string d;
		cin >> d;
		cout << make(line, find(line, d)) << endl;
	}
	return 0;
}

int find(vector<item> &line, string name) {
	int sp = 0;
	int up = line.size();
	while (true) {
		int tmp = (sp + up) / 2;
		if (line[tmp].name==name) {
			return tmp;
		}
		else if (line[tmp].name < name) {
			sp = tmp + 1;
		}
		else {
			up = tmp;
		}
	}
}

int make(vector<item> &line, int place) {
	int res = line[place].price;
	if (!line[place].recipe.empty()) {
		int res2 = 0;
		for (int i = 0, nmax = line[place].recipe.size(); i < nmax; ++i) {
			res2 += make(line, line[place].recipe[i]);
		}
		return min(res, res2);
	}
	return res;
}