#include <iostream>
#include <string>
#include <map>
using namespace std;
int main( void )
{
	map < string, int > T;
	int n;
	while ( cin >> n && n != 0 )
	{
		for ( int i = 0; i < n; i++ )
		{
			string item;
			int value;
			cin >> item >> value;

			T[item] = value;
		}

		int m;
		cin >> m;
		for ( int i = 0; i < m; i++ )
		{
			string item;
			int k;
			cin >> item >> k;

			int sum = 0;
			for ( int j = 0; j < k; j++ )
			{
				string additem;
				cin >> additem;
				sum += T[additem];
			}

			T[item] = min( T[item], sum );
		}

		string getitem;
		cin >> getitem;

		cout << T[getitem] << endl;
	}
	return 0;
}