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

int main() {
	int n;
	while(cin >> n, n) {
		vector<int> v;
		map<string,int> index;
		bool f[128] = {0};

		for(int i = 0; i < n; i++) {
			string s;
			int c;
			cin >> s >> c;
			v.push_back(c);
			index[s] = i;
		}

		int m;
		int edge[128][128] = {0};
		cin >> m;
		for(int i = 0; i < m; i++) {
			int k;
			string s0, s1;
			cin >> s0 >> k;
			for(int j = 0; j < k; j++) {
				string s1;
				cin >> s1;
				edge[index[s0]][index[s1]] = 1;
//				cout << index[s0] << " " << index[s1] << endl;
				f[index[s0]] = true;
			}
		}

		for(int i = 0; i < v.size(); i++) {
			for(int j = 0; j < v.size(); j++) {
				if(f[j]) {
					int cost = 0;
					for(int k = 0; k < v.size(); k++) {
						if(edge[j][k]) {
							cost += v[k];
						}
					}
					v[j] = min(v[j], cost);
				}
			}
		}

		string want;
		cin >> want;

		cout << v[index[want]] << endl;
	}
}