// 2011/08/02 Tazoe

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

int min_cost(string str_itm, map<string, int>& itm, map<string, vector<string> >& rcp)
{
	// VsªÈ¢ê
	if(rcp.find(str_itm)==rcp.end())
		return itm[str_itm];

	int cost_rcp = 0;
	for(vector<string>::iterator p=rcp[str_itm].begin(); p<rcp[str_itm].end(); p++)
		cost_rcp += min_cost(*p, itm, rcp);

	if(cost_rcp<itm[str_itm])
		return cost_rcp;
	else
		return itm[str_itm];
}

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		map<string, int> itm;
		for(int i=0; i<n; i++){
			string str_i;
			int p;
			cin >> str_i >> p;
			itm[str_i] = p;
		}

		int m;
		cin >> m;

		map<string, vector<string> > rcp;
		for(int i=0; i<m; i++){
			string str_o;
			int k;
			cin >> str_o >> k;
			for(int j=0; j<k; j++){
				string str_q;
				cin >> str_q;
				rcp[str_o].push_back(str_q);
			}
		}

		string str_itm;
		cin >> str_itm;

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

	return 0;
}