#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>

#define mp make_pair
#define rep(i,n) for(int i = 0 ; i < n ; i++)

using namespace std;

class pred {
	string str;
public:
	pred(const string& _str) : str(_str) {}
	bool operator()( const pair< string , int > pr ) { return pr.first == str; }
};

int main(){
	
	int n;
	string s_ans;
	
	cin >> n;
	while(n)
	{
		vector< pair<string , int> > item;
		vector< pair<string , int> > :: iterator ite;
		
		rep(i,n) 
		{
			string a;
			int	b;
			cin >> a >> b;
			item.push_back( mp( a , b ) );
		}
		
		cin>>n;
		
		rep(i,n)
		{
			string s;
			int m , goukei=0;
			cin >> s >>m;
			rep(i,m)
			{
				string dummy;
				cin >> dummy;
				ite = find_if( item.begin() , item.end() , pred( dummy ) );
				goukei += (*ite).second;
			}
			ite = find_if( item.begin() , item.end() , pred( s ) );
			if( (*ite).second > goukei )
			{
				ite = remove_if( item.begin() , item.end(), pred( s ) );
				item.erase( ite );
				item.push_back( mp( s , goukei )  );
			}
		}
		
		cin>>s_ans;
		ite = find_if( item.begin() , item.end() , pred( s_ans ) );
		cout << (*ite).second << endl;
		cin>>n;
	}
	return 0;
}