#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, p[109], dp[109]; string str; map<string, int> x; vector<int> G[109];
int solve(int pos) {
	if (dp[pos] != -1) return dp[pos];
	int ret = p[pos], sum = 0;
	for (int i : G[pos]) sum += solve(i);
	if (G[pos].size()) ret = min(ret, sum);
	return dp[pos] = ret;
}
int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> str >> p[i];
			x[str] = i;
		}
		cin >> m;
		fill(G, G + n, vector<int>());
		for (int i = 0; i < m; i++) {
			cin >> str; int z = x[str];
			int e; cin >> e;
			for (int j = 0; j < e; j++) {
				cin >> str;
				G[z].push_back(x[str]);
			}
		}
		cin >> str;
		fill(dp, dp + n, -1);
		cout << solve(x[str]) << endl;
	}
	return 0;
}