#include<iostream>
#include<map>
#include<string>
using namespace std;
int main() {
	int n;
	while (cin >> n&&n != 0) {
		map<string, int>a;
		for (int i = 0; i < n; i++) {
			string b;
			int c;
			cin >> b >> c;
			a.insert(map<string, int>::value_type(b, c));
		}
		cin >> n;
		for (int i = 0; i < n; i++) {
			string b, c[100], d,h="";
			int e,f=0;
			getline(cin, d);
			d += " ";
			for (int i = 0; i < d.length(); i++) {
				if (d[i] == ' ') {
					if (f == 0) b = h;
					else if (f == 1) e = atoi(h.c_str());
					else c[f - 2] = h;
					h = "";
					f++;
				}
			}
			int g = 0;
			for (int j = 0; j < e; j++) {
				g += a[c[j]];
			}
			if (g < a[b]) {
				a[b] = g;
			}
		}
		string k;
		cin >> k;
		cout << a[k] << endl;
	}
}