#include<iostream>
#include<string>
#include<vector>
using namespace std;
pair<string, int>X[1000];
vector<int>Y[1000];
int dp[1000];
int N, M, C, Q; string S;
int main() {
	while (true) {
		for (int i = 0; i < 1000; i++) {
			dp[i] = 0;
			Y[i].clear();
			X[i] = make_pair("", 0);
		}
		cin >> N;
		if (N == 0) { break; }
		for (int i = 0; i < N; i++) {
			cin >> X[i].first >> X[i].second;
			dp[i] = X[i].second;
		}
		cin >> M;
		for (int i = 0; i < M; i++) {
			cin >> S;
			for (int j = 0; j < N; j++) {
				if (S == X[j].first) {
					C = j;
				}
			}
			cin >> Q;
			for (int j = 0; j < Q; j++) {
				cin >> S;
				for (int k = 0; k < N; k++) {
					if (X[k].first == S) {
						Y[C].push_back(k);
					}
				}
			}
		}
		for (int i = 0; i < N * 2; i++) {
			for (int j = 0; j < N; j++) {
				int sum = 0;
				if(Y[j].size()==0){
					continue;
				}
				for (int k = 0; k < Y[j].size(); k++) {
					sum += dp[Y[j][k]];
				}
				if (dp[j] > sum) {
					dp[j] = sum;
				}
			}
		}
		cin >> S;
		for (int i = 0; i < N; i++) {
			if (S == X[i].first) {
				C = i;
			}
		}
		cout << dp[C] << endl;
	}
	return 0;
}