#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

map<string, int> value;
map<string, vector<string> > recipe;

int recur(string item)
{
	if (!recipe[item].size())
		return value[item];

	int s = 0;
	for (int i = 0; i < recipe[item].size(); ++i)
		s += recur(recipe[item][i]);
	return min(value[item], s);
}
int main()
{
	int n;
	while (cin >> n, n)
	{
		value.clear();
		recipe.clear();
		for (int i = 0; i < n; ++i)
		{
			string a;
			int v;
			cin >> a >> v;
			value[a] = v;
		}
		int m;
		cin >> m;
		for (int i = 0; i < m; ++i)
		{
			string a;
			int k;
			cin >> a >> k;
			for (int j = 0; j < k; ++j)
			{
				string b;
				cin >> b;
				recipe[a].push_back(b);
			}
		}

		string item;
		cin >> item;
		cout << recur(item) << endl;
	}

	return 0;
}