#include <bits/stdc++.h>
using namespace std;

typedef pair<string, int>P;

int main(){
	while(1){
	int n;
	cin >> n;
	if(n == 0) return 0;
	vector<P>v;
	for(int i = 0; i < n; i++){
		string s;
		int num;
		cin >> s >> num;
		v.push_back(P(s, num));
	}
	int m;
	cin >> m;
	vector<int>e[1005];
	for(int i = 0; i < m; i++){
		string s;
		cin >> s;
		int num;
		cin >> num;
		string now;
		for(int j = 0; j < num + 1; j++){
			if(j == 0) now = s;
			else cin >> now;
			for(int k = 0; k < n; k++){
				if(now == v[k].first){
					e[i].push_back(k);
				}
			}
		}
	}
	while(1){
		bool foo = false;
		for(int i=0;i<m;i++){
			int sum = 0;
			for(int j = 1; j < e[i].size(); j++){
				sum += v[e[i][j]].second;
			}
			if(v[e[i][0]].second > sum){
				v[e[i][0]].second = sum;
				foo=true;
			}
		}
		if(!foo) break;
	}
	string s;
	cin >> s;
	for(int i = 0; i < n; i++){
		if(v[i].first == s){
			cout << v[i].second << endl;
			continue;
		}
	}
}
	return 0;
}

