#include <iostream>
#include <string>
#include <vector>
#include <map>
#define MAX_N 100
#define MAX_M 100
#define MAX_K 100

using namespace std;

typedef struct _Recipe{
	string NewItemName;
	int SourceItemNumber;
	vector<string> SourceItemName;
}Recipe;

typedef struct _Item{
	string ItemName;
	int Price;
}Item;


int main(void){
	int n, m, min_cost[MAX_N];
	string require;
	Item itm[MAX_N];
	Recipe rcp[MAX_M];
	map<string,int> ItemName_To_Index;
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		for(int i = 0;i < n;i++){
			cin >> itm[i].ItemName >> itm[i].Price;
			min_cost[i] = itm[i].Price;
			ItemName_To_Index.insert(map<string,int>::value_type(itm[i].ItemName,i));
			//cout << "価格 " << itm[i].Price << endl;
		}
		cin >> m;
		for(int i = 0;i < m;i++){
			cin >> rcp[i].NewItemName >> rcp[i].SourceItemNumber;

			for(int j = 0;j < rcp[i].SourceItemNumber;j++){
				string tmp;
				cin >> tmp;
				rcp[i].SourceItemName.push_back(tmp);
			}

		}
		cin >> require;



		for(int i = 0;i < m;i++){
			for(int j = 0;j < m;j++){
				int sum = 0;
				for(int k = 0;k < rcp[j].SourceItemNumber;k++){
					sum += min_cost[ItemName_To_Index[rcp[j].SourceItemName[k]]];
				}
				min_cost[ItemName_To_Index[rcp[j].NewItemName]] = min(sum,min_cost[ItemName_To_Index[rcp[j].NewItemName]]);
			}
		}

		cout << min_cost[ItemName_To_Index[require]] << endl;
		for(int i = 0;i < m;i++){
			rcp[i].SourceItemName.clear();
		}
		ItemName_To_Index.clear();
		/*for(int i = 0;i < n;i++){
			cout << itm[i].ItemName << " 最安値 " << min_cost[i] << endl;
		}*/
	}

	return 0;
}