// 2014/10/27 Tazoe

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

int min_cost(string r, map<string, int>& itm, map<string, vector<string> >& rcp)
{
	if(rcp.find(r)==rcp.end())
		return itm[r];

	int sum = 0;
	for(int i=0; i<rcp[r].size(); i++){
		sum += min_cost(rcp[r][i], itm, rcp);
	}

	if(sum<itm[r])
		return sum;
	else
		return itm[r];
}

int main()
{
	while(true){
		int n;
		cin >> n;

		if(n==0)
			break;

		map<string, int> itm;
		for(int i=0; i<n; i++){
			string s;
			int p;
			cin >> s >> p;

			itm[s] = p;
		}

		int m;
		cin >> m;

		map<string, vector<string> > rcp;
		for(int i=0; i<m; i++){
			string o;
			int k;
			cin >> o >> k;

			for(int j=0; j<k; j++){
				string q;
				cin >> q;

				rcp[o].push_back(q);
			}
		}

		string r;
		cin >> r;

		cout << min_cost(r, itm, rcp) << endl;
	}

	return 0;
}