#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int N;


class State
{

public:
	string item;
	int cost;

	State(string item, int cost):item(item),cost(cost){}

};

int main(void)
{
	while(cin >> N, N){

		vector<State> items;
		map<string, int> itemmap;
		map<string, vector<string> > itemR;

		string itemstr,itemstr2;
		string queryitem;
		int money;
		for(int i = 0; i < N; i++){
			cin >> itemstr >> money;
			State st(itemstr,money);
			items.push_back(st);
			itemmap[st.item] = st.cost;
		}
		cin >> N;

		int M;
		for(int i = 0; i < N; i++){
			cin >> itemstr;
			cin >> M;

			
			while(M > 0){
				cin >> itemstr2;
				itemR[itemstr].push_back(itemstr2);
				M--;
				
			}
				for(int j = 0; j < items.size(); j++){
					int sum = 1 << 28;
					int mcost = items[j].cost;

				//	printf("\n");
					if(itemR[items[j].item].size()){
						sum = 0;
						for(int k = 0; k < itemR[items[j].item].size();  k++){
							string No = itemR[items[j].item][k];
							sum += itemmap[No];
			//				cout << No << ' ';
						}
						items[j].cost = min(mcost, sum);
						itemmap[items[j].item] = min(mcost, sum);
					//	cout << items[j].item << ' ' << mcost << ' ' << sum << endl;
					
				}

			}
		}
		

		cin >> queryitem;

		int ans = itemmap[queryitem];


		cout << ans << endl;

	//	for(int i = 0; i < items.size(); i++){
	//		cout << items[i].item << items[i].cost << endl;;
	//	}




	}



	return 0;
}