#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
using namespace std;
string item[110];
int cost[110][110];
int solve(int x, int n) {
	int ans = 0;
	bool f = 1;
	rep(i,n) {
		if(i==x) continue;
		if(cost[x][i]!=-1) {
			f=0;
			ans += solve(i,n);
		}
	}
	if(f) return cost[x][x];
	return min(cost[x][x], ans);

}
int main(){
	int n, k, m;
	string s;
	while(cin >> n, n) {
		rep(i,110) {
			rep(j,110) {
				cost[i][j] = -1;
			}
		}
		rep(i,n) {
			cin >> item[i] >> cost[i][i];
		}
		cin >> k;
		rep(i,k) {
			cin >> s >> m;
			rep(j,100) {
				if(item[j] == s) {
					rep(p,m) {
						cin >> s;
						rep(q,100) {
							if(item[q] == s) {
								cost[j][q] = cost[q][q];
								break;
							}
							
						}
					}
					break;
				}
			}
		}
		cin >> s;
		rep(i,100) {
			if(item[i] == s) {
				cout << solve(i,n) << endl;
				break;
			}
		}
	}
}