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

int main(){
	int n , m , k , p , psum;
	string a , b;
	string want;
	while(cin >> n , n){
		map<string , int> item;
		for(int i=0;i<n;++i){
			cin >> a >> p ;
			item.insert(map<string , int>::value_type( a , p ));
		}
		cin >> m;
		for(int i=0;i<m;++i){
			psum=0;
			cin >> a >> k; //??¬??????a ???????????°k
			for(int j=0 ; j<k ;++j){
				cin >> b;  //??¬??????
				if(item.find(b)!=item.end()) psum+=item[b];
			}
				if(psum < item[a]) {
					item[a]=psum;//???????????´
				}
		}
		cin >> want;
		cout << item[want] << endl;
	}	
	return 0;
}