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

int main() {
	int a;
	while (scanf("%d", &a), a) {
		map<string, int>b;
		for (int c = 0; c < a; c++) {
			string d; int e;
			cin >> d;
			scanf("%d", &e);
			b[d] = e;
		}
		int f;
		scanf("%d", &f);
		map<string, vector<string>>h;
		for (int g = 0; g < f; g++) {
			string i; int j;
			cin >> i;
			scanf("%d", &j);
			vector<string>k(j);
			for (int l = 0; l < j; l++)cin >> k[l];
			h[i] = k;
		}
		for (int x = 0; x < a; x++) {
			bool y = false;
			for (auto z = h.begin(); z != h.end(); z++) {
				int q = 0;
				for (int p = 0; p < z->second.size(); p++) {
					q += b[z->second[p]];
				}
				if (q < b[z->first]) {
					y = true;
					b[z->first] = q;
				}
			}
			if (!y)break;
		}
		string s;
		cin >> s;
		cout << b[s] << endl;
	}
}