#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <vector>
using namespace std;

int n, m, k;
string o;
vector<pair<string, vector<string> > > mix;
map<string, int> item;

int sum(vector<string> s) {
	int p = 0;
	for (int i = 0; i < s.size(); i++) {
		p += item[s[i]];
	}
	return p;
}

void update() {
	int p;
	for (int i = 0; i < m; i++) {
		p = sum(mix[i].second);
		if (p < item[mix[i].first]) {
			item[mix[i].first] = p;
		}
	}
}

int main(){
	while (cin >> n && n != 0) {
		item = map<string, int>();
		for (int i = 0; i < n; i++) {
			cin >> o >> k;
			item[o] = k;
		}
		cin >> m;
		mix = vector<pair<string, vector<string> > >(m);
		for (int i = 0; i < m; i++) {
			cin >> mix[i].first >> k;
			for (int j = 0; j < k; j++) {
				cin >> o;
				mix[i].second.push_back(o);
			}
		}
		for (int i = 0; i < 10000; i++) {
			update();
		}
		cin >> o;
		cout << item[o] << endl;
	}

	return 0;
}