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

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 productId = nameList.find( o )->second;
			for( int j = 0; j < k; ++j ){
				string q;
				cin >> q;
				int materialId = nameList.find( q )->second;
				recipeList[ productId ].push_back( materialId );
			}
		}
		vector< int > depthList( n, -1 );
		queue< int > updateList;
		for( int i = 0; i < n; ++i ){
			updateList.push( i );
		}
		while( !updateList.empty() ){
			int id = updateList.front();
			updateList.pop();
			if( recipeList[ id ].empty() ){
				depthList[ id ] = 0;
				continue;
			}
			int size = recipeList[ id ].size();
			int d = -1;
			for( int i = 0; i < size; ++i ){
				d = max( d, depthList[ recipeList[ id ][ i ] ] );
			}
			if( d > -1 ){
				depthList[ id ] = d + 1;
			}else{
				updateList.push( id );
			}
		}
		vector< pair< int, int > > sortedList;
		for( int i = 0; i < n; ++i ){
			sortedList.push_back( make_pair( depthList[ i ], i ) );
		}
		sort( sortedList.begin(), sortedList.end() );

		for( int i = 0; i < (int)sortedList.size(); ++i ){
			int id = sortedList[ i ].second;
			vector< int > recipe = recipeList[ id ];
			if( recipe.empty() ){
				continue;
			}
			long long int value = 0;
			for( int j = 0; j < (int)recipe.size(); ++j ){
				value += valueList[ recipe[ j ] ];
			}
			if( value < valueList[ id ] ){
				valueList[ id ] = value;
			}
		}

		string ansName;
		cin >> ansName;
		cout << valueList[ nameList.find( ansName )->second ] << endl;
	}
	return 0;
}