#include<iostream>
#include<string>

using namespace std;

struct Item {
	string name;
	int value;
};

Item item[100];

int newValue[100];

struct Recipe {
	int item;
	int len;
	int suppli[100];
};

Recipe recipe[100];


int main () {

	while(true) {
		for ( int i=0; i<100; i++ ) {
			item[i].value = -1;

			for ( int j=0; j<100; j++ ) {
				recipe[i].suppli[j] = 0;
			}
		}


		int itemNum;
		cin >> itemNum;

		if ( itemNum == 0 )
			break;

		for ( int i=0; i<itemNum; i++ ) {
			cin >> item[i].name;
			cin >> item[i].value;
			//cout << item[i].name << item[i].value << endl;
		}
		
		int recipeNum;
		cin >> recipeNum;
		for ( int i=0; i<recipeNum; i++ ) {
			string find;
			cin >> find;
			for ( int j=0; j<itemNum; j++ ) {
				if ( find.compare(item[j].name)==0 ) {
					recipe[i].item = j;
					break;
				}
			}

			cin >> recipe[i].len;
			//			cout << "len: " << recipe[i].len << endl;

			for ( int j=0; j<recipe[i].len; j++ ) {
				cin >> find;
				//				cout << "Find: " << find << " ";
				for ( int k=0; k<itemNum; k++ ) {
					//cout << "Now: " << item[k].name << endl;
					
					if (find.compare(item[k].name)==0) {
						//cout << "Found: " << item[j].name << endl << endl;
						recipe[i].suppli[j] = k;
						break;
					}
				}
			}
		}



		for ( int i=0; i<recipeNum; i++ ) {
			int sum = 0;
			for ( int j=0; j<recipe[i].len; j++ ) {
				sum += item[recipe[i].suppli[j]].value;
			}
			if ( item[recipe[i].item].value > sum )
				item[recipe[i].item].value = sum;
		}

		//cout << endl;
		for ( int i=0; i<itemNum; i++ ) {
			//cout << item[i].name << item[i].value << endl;
		}
			
		string find;
		cin >> find;

		for ( int i=0; i<itemNum; i++ ) {
			if ( find.compare(item[i].name)==0 ) {
				cout << item[i].value << endl;
				break;
			}
		}
	}
}