#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <queue>

using namespace std;

vector<int> tree[200];
int cost[200];

int min_cost(int index){
	if(tree[index].size() == 0){
		return cost[index];
	}
	int sum = 0;
	int siz = tree[index].size();
	for(int i = 0; i < siz; i++){
		sum += min_cost(tree[index].at(i));
	}
	return min(sum, cost[index]);
}

int main(){
	
	string name[200];
	
	int n, m;
	
	while(true){
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> name[i] >> cost[i];
			tree[i].erase(tree[i].begin(), tree[i].end());
		}
		
		cin >> m;
		
		for(int i = 0; i < m; i++){
			string str;
			int num;
			cin >> str >> num;
			int index = 0;
			for(int j = 0; j < n; j++){
				if(str == name[j]){
					index = j;
					break;
				}
			}
			for(int j = 0; j < num; j++){
				string str2;
				cin >> str2;
				for(int k = 0; k < n; k++){
					if(str2 == name[k]){
						tree[index].push_back(k);
						break;
					}
				}
			}
		}
		
		string str;
		cin >> str;
		int index;
		
		for(int i = 0; i < n; i++){
			if(str == name[i]){
				index = i;
				break;
			}
		}
		
		cout << min_cost(index) << endl;
		
	}
	
	return 0;
}