#include <iostream>
#include <map>

using namespace std;

int main()
{
	int n,n2,n3;
	string name,lname;
	map<string,int> item;
	map<string,int> le;

	while( cin >> n && n )
	{
		for(int i=0;i<n;i++)
		{
			cin >> name;
			cin >> item[name];
		}
		cin >> n2;
		for(int i=0;i<n2;i++)
		{
			cin >> lname >> n3;
			for(int i=0;i<n3;i++)
			{
				cin >> name;
				if(le[name])
					le[lname]+=le[name];
				else 
					le[lname]+=item[name];
			}
		}
		cin >> lname;
		if(!le[lname])cout << item[lname] << endl;
		else cout << le[lname] << endl;
	}
}