#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;
typedef pair<string, int> P;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		map<string, int> Map;
		for(int i = 0; i < n; ++i)
		{
			string s;
			int t;
			cin >> s >> t;
			Map[s] = t;
		}
		int m;
		cin >> m;
		vector<string> item(m);
		vector<int> Vec(m);
		vector< vector<string> > str(m);
		for(int i = 0; i < m; ++i)
		{
			cin >> item[i] >> Vec[i];
			for(int j = 0; j < Vec[i]; ++j)
			{
				string s;
				cin >> s;
				str[i].push_back(s);
			}
		}
		while(true)
		{
			bool update = false;
			for(int i = 0; i < m; ++i)
			{
				int sum = 0;
				for(int j = 0; j < Vec[i]; ++j)
				{
					sum += Map[str[i][j]];
				}
				if(Map[item[i]] > sum)
				{
					Map[item[i]] = sum;
					update = true;
				}
			}
			if(update == false)
			{
				break;
			}
		}
		string Target;
		cin >> Target;
		cout << Map[Target] << endl;
	}
}

int main()
{
	solve();
	return(0);
}