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

typedef pair<int,string> pis;

int main() {
	int n;
	while(cin >> n, n) {
		vector<pis> v;
		for(int i = 0; i < n; i++) {
			string s;
			int c;
			cin >> s >> c;
			v.push_back(pis(c,s));
		}
		sort(v.begin(), v.end());

		int m;
		vector<vector<string> > r;
		cin >> m;
		for(int i = 0; i < m; i++) {
			int k;
			string s;
			cin >> s;
			cin >> k;
			r.push_back(vector<string>());
			r[i].push_back(s);
			for(int j = 0; j < k; j++) {
				cin >> s;
				r[i].push_back(s);
			}
		}

		for(int i = 0; i < v.size(); i++) {
			for(int j = 0; j < r.size(); j++) {
				if(v[i].second == r[j][0]) {
					int nc = 0;
					for(int k = 1; k < r[j].size(); k++) {
						for(int l = 0; l < v.size(); l++) {
							if(r[j][k] == v[l].second) {
								nc += v[l].first;
							}
						}
					}
					v[i].first = min(v[i].first, nc);
				}
			}
		}

		string want;
		cin >> want;
		for(int i = 0; i < v.size(); i++) {
			if(v[i].second == want) {
				cout << v[i].first << endl;
				break;
			}
		}
	}
}