#include<algorithm>
#include<iostream>
#include<map>
#include<string>

int n;
std::map<std::string, int> hash;

int main()
{
	while( std::cin >> n, n )
	{
		hash.clear();

		for( int i = 0; i != n; ++i )
		{
			std::string s;
			int p;
			std::cin >> s >> p;
			hash.insert( std::make_pair( s, p ) );
		}

		int m;
		std::cin >> m;
		for( int i = 0; i != m; ++i )
		{
			std::string o;
			int k;
			std::cin >> o >> k;
			
			int p = 0;
			for( int j = 0; j != k; ++j )
			{
				std::string q;
				std::cin >> q;
				p += hash[q];
			}

			hash[o] = std::min( hash[o], p );
		}

		std::string item;
		std::cin >> item;
		std::cout << hash[item] << std::endl;
	}

	return 0;
}