// 2010/12/22 Tazoe

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

int min_cost(string item, map<string, int>& items, map<string, vector<string>>& recipes)
{
	if(recipes.find(item)==recipes.end())
		return items[item];

	int cost_recipes = 0;
	for(vector<string>::iterator p=recipes[item].begin(); p<recipes[item].end(); p++)
		cost_recipes += min_cost(*p, items, recipes);

	if(cost_recipes<items[item])
		return cost_recipes;
	else
		return items[item];
}

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

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

		int m;
		cin >> m;

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

		string item;
		cin >> item;

		cout << min_cost(item, items, recipes) << endl;
	}

	return 0;
}