#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

long long int updateValue( int n, vector< long long int >& valueList, vector< vector< int > >& recipeList, vector< bool >& updatedList )
{
	if( updatedList[ n ] ){
		return valueList[ n ];
	}
	updatedList[ n ] = true;
	vector< int > recipe = recipeList[ n ];
	if( recipe.empty() ){
		return valueList[ n ];
	}
	long long int value = 0;
	for( int i = 0; i < (int)recipe.size(); ++i ){
		int id = recipe[ i ];
		value += updateValue( id, valueList, recipeList, updatedList );
	}
	if( value < valueList[ n ] ){
		valueList[ n ] = value;
	}
	return valueList[ n ];
}

int main()
{
	int n;
	while( cin >> n && n ){
		int id = 0;
		map< string, int > nameList;
		vector< long long int > valueList( n, 0 );
		for( int i = 0; i < n; ++i ){
			string s;
			int v;
			cin >> s >> v;
			int d = id++;
			nameList.insert( make_pair( s, d ) );
			valueList[ d ] = v;
		}
		int m;
		vector< vector< int > > recipeList( n, vector< int >() );
		cin >> m;
		for( int i = 0; i < m; ++i ){
			string o;
			int k;
			cin >> o >> k;
			int count = nameList.count( o );
			if( count == 0 ){
				continue;
			}
			int productId = nameList[ o ];
			for( int j = 0; j < k; ++j ){
				string q;
				cin >> q;
				count = nameList.count( q );
				if( count == 0 ){
					recipeList[ productId ] = vector< int >();
					break;
				}
				int materialId = nameList[ q ];
				recipeList[ productId ].push_back( materialId );
			}
		}
		vector< bool > updatedList( n, false );
		for( int i = 0; i < n; ++i ){
			updateValue( i, valueList, recipeList, updatedList );
		}

		string ansName;
		cin >> ansName;
		cout << valueList[ nameList[ ansName] ] << endl;
	}
	return 0;
}