#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;


int			price[100];
bool		decided[100];
vector<int>	recipe[100];

int dfs(int index)
{
	if(decided[index])
	{
		return price[index];
	}
	if(recipe[index].empty())
	{
		decided[index] = true;
		return price[index];
	}

	int newprice=0;
	for(size_t i=0; i<recipe[index].size(); i++)
	{
		newprice+=dfs(recipe[index][i]);
	}
	price[index] = (newprice<price[index]?newprice:price[index]);

	decided[index] = true;
	return price[index];
}

int main()
{
	int N,M,p,l,idx;
	string s;
	while(cin >> N, N)
	{
		map<string,int> s2i;
		for(int i=0; i<N; i++)
		{
			cin >> s >> p;
			s2i[s] = i;

			price[i]   = p;
			decided[i] = false;
			recipe[i].clear();
		}

		cin >> M;
		for(int i=0; i<M; i++)
		{
			cin >> s >> l;
			idx = s2i[s];
			while(l--)
			{
				cin >> s;
				recipe[idx].push_back(s2i[s]);
			}
		}

		cin >> s;
		cout << dfs(s2i[s]) << endl;		
	}

	return 0;
}