#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
int main() {
	int n;
	while (cin >> n&&n != 0) {
		string a[100];
		int b[100];
		bool c[100];
		vector<int>d[100];
		list<int>m2;
		for (int i = 0; i<n; i++) {
			cin >> a[i] >> b[i];
			c[i] = false;
		}
		int m;
		cin >> m;
		for (int i = 0; i<m; i++) {
			string o, q;
			int k;
			cin >> o >> k;
			int e;
			for (e = 0; e<n; e++) {
				if (a[e] == o)
					break;
			}
			c[e] = true;
			m2.push_back(e);
			for (int j = 0; j<k; j++) {
				cin >> q;
				for (int z = 0; z<n; z++) {
					if (a[z] == q) {
						d[e].push_back(z);
						break;
					}
				}
			}
		}
		while (m2.size()>0) {
			list<int>::iterator it = m2.begin(), it2;
			for (int i = 0; i<(int)m2.size();) {
				int e = (*it), sum = 0;
				bool l = true;
				for (int j = 0; j<(int)d[e].size(); j++) {
					if (c[d[e][j]]) {
						l = false;
						goto stop;
					}
					sum += b[d[e][j]];
				}
				b[e] = min(b[e], sum);
				it2 = it;
				it++;
				m2.erase(it2);
				c[e] = false;
			stop:;
				if (!l) {
					it++;
					i++;
				}
			}
		}
		string t;
		cin >> t;
		for (int i = 0; i<n; i++) {
			if (a[i] == t) {
				cout << b[i] << endl;
				break;
			}
		}
	}
}