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

string name[100];
string buy;
map<string, int> list;
int n,m;

int main(){
	while(true){

		cin >> n;
		if(!n)
			return 0;
		list.clear();

		for(int i=0; i<n; i++){
			cin >> name[i];
			cin >> list[name[i]];
		}

		cin >> m;
		for(int i=0; i<m; i++){
			int tmp;
			int price = 0;
			string temp;
			cin >> temp >> tmp;
			for(int k=0; k<tmp; k++){
				string s;
				cin >> s;
				price += list[s];
			}
			if(price < list[temp])
				list[temp] = price;
		}

		cin >> buy;
		cout << list[buy] << endl;
	}
}