#include <iostream>
#include <string>

using namespace std;

struct Item{
	string name;
	int price;
	int item_number;
	bool cook_flag;
};

struct Recipe{
	int item_number[100];
	int material;
};
Item item[100];
Recipe recipe[100];

int min(Item _item, int _num){
	if (!_item.cook_flag){
		return _item.price;
	}
	else{
		int sum = 0;
		for (int i = 0; i < recipe[_num].material; i++){
			int num = recipe[_num].item_number[i];
			int cook_price = min(item[num], num);
			if (cook_price > item[num].price) sum += item[num].price;
				else sum += cook_price;
		}
		if (sum > _item.price) sum = _item.price;
		return sum;
	}
}

int get_num(string name){
	for (int i = 0; i < 100; i++){
		if (name == item[i].name){
			return i;
		}
	}

}

int main(){
	while (1){
		int n;
		cin >> n;
		if (!n)break;
		for (int i = 0; i < n; i++){
			cin >> item[i].name;
			cin >> item[i].price;
			item[i].item_number = i;
			item[i].cook_flag = false;
		}

		int m;
		cin >> m;
		string o;
		int k;
		for (int i = 0; i < m; i++){
			cin >> o;
			int o_num = get_num(o);
			item[o_num].cook_flag = true;
			cin >> k;
			recipe[o_num].material = k;

			for (int j = 0; j < k; j++){
				string name;
				cin >> name;
				int num = get_num(name);
				recipe[o_num].item_number[j] = num;
			}
		}

		string req;
		cin >> req;
		int num = get_num(req);
		int ans;
		ans = min(item[num], num);
		cout << ans<<"\n";
	}
	return 0;
}