#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n;
	while (cin >> n, n) {
		map<string, int>cost;
		map<string, vector<string>>c;
		rep(i, n) {
			string s; int d; cin >> s >> d;
			cost[s] = d;
		}
		int m; cin >> m;
		rep(i, m) {
			string s; int d; cin >> s >> d;
			rep(j, d) {
				string S; cin >> S;
				c[s].push_back(S);
			}
		}
		rep(i, n) {
			for (auto&p : cost) {
				if (!c[p.first].empty()) {
					int cnt = 0;
					for (auto j : c[p.first])cnt += cost[j];
					p.second = min(p.second, cnt);
				}
			}
		}
		string s; cin >> s;
		cout << cost[s] << endl;
	}
}