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

using namespace std;

void solve()
{
	int n;
	while (cin >> n, n)
	{
		map<string, int> table;
		for (int i = 0; i < n; ++i)
		{
			string name;
			int cost;
			cin >> name >> cost;
			table[name] = cost;
		}
		int m;
		cin >> m;
		for (int i = 0; i < m; ++i)
		{
			string name;
			int num;
			cin >> name >> num;
			int sum = 0;
			for (int j = 0; j < num; ++j)
			{
				string material;
				cin >> material;
				sum += table[material];
			}
			table[name] = min(table[name], sum);
		}
		string name;
		cin >> name;
		cout << table[name] << endl;
	}
}

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