#include <iostream>
#include <sstream>
using namespace std;

long int match(string,int);
long int value_min(string,long int);

string item_list[100],recipe_to[100];
string recipe_from[100][100];
long int item_value[100];
//long int value_min;

int recipe_num,list_num;
int recipe_to_num[100];


int main(){
//	string recipe_from[100][100];
//	int recipe_num,list_num;//,value_min;
//	int recipe_to_num[100];
	string purpose,item;

	while(cin >> list_num && list_num != 0){
		//		cout << list_num << endl;
		//f[^ÌÇÝÝ
		for(int i=0;i<list_num;i++){
			cin >> item_list[i] >> item_value[i];
			//			cout << item_list[i] << item_value[i] << endl;
		}
		cin >> recipe_num;
		for(int i=0;i<recipe_num;i++){
			cin >> recipe_to[i];
			cin >> recipe_to_num[i];
			for(int j=0;j<recipe_to_num[i];j++){
				cin >> recipe_from[i][j];
			}
		}
		//
		//		for(int i=0;i<recipe_num;i++){
		////			cout << recipe_to[i] << ":";
		//			for(int j=0;j<recipe_to_num[j];j++){
		////				cout << recipe_from[i][j] << " ";
		//			}
		////			cout << endl;
		//		}
		cin >> purpose;
		//		cout << purpose << endl;
		//±±ÜÅf[^ÌÇÝÝ



		cout << value_min(purpose,match(purpose,list_num)) << endl;
	}

}

long int match(string item,int num){
	for(int i=0;i<num;i++){
		if(item == item_list[i]){
			return item_value[i];
		}
	}
}

long int value_min(string item,long int value){
	for(int i=0;i<recipe_num;i++){
		//value = 0;
			if(item == recipe_to[i]){
				long int sum = 0;
				for(int j=0;j<recipe_to_num[i];j++){
					sum += value_min(recipe_from[i][j],match(recipe_from[i][j],list_num));
//					sum += match(recipe_from[i][j],list_num)
				}
				value = (sum > value ? value :  sum);
			}
		}
	return value;
}