#include <unordered_map>
#include <string>
#include <vector>
#include <iostream>

class Item {
public:
	Item(const std::string &n, const int &p) :name(n), price(p), made_from() {};
	Item() :name(), price(), made_from() {};
	void add_recipe(Item* item) {
		made_from.push_back(item);
	}
	int get_price() {
		if (made_from.size() == 0) {
			return price;
		}
		else {
			int temp = 0;
			for (auto i : made_from) {
				temp += i->get_price();
			}
			return (price > temp) ? temp : price;
		}
	}
private:
	std::string name;
	int price;
	std::vector<Item*> made_from;
};
int main() {
	int n = 1;
	std::cin >> n;
	while (n != 0) {
		std::unordered_map<std::string, Item> map;
		for (auto i = 0; i < n; ++i) {
			std::string name;
			int price;
			std::cin >> name >> price;
			map[name] = Item(name, price);
		}
		std::cin >> n;
		for (auto i = 0; i < n; ++i) {
			std::string item;
			std::cin >> item;
			int m;
			std::cin >> m;
			for (auto j = 0; j < m; ++j) {
				std::string factor;
				std::cin >> factor;
				map[item].add_recipe(&map[factor]);
			}
		}
		std::string order;
		std::cin >> order;
		std::cout << map[order].get_price() << std::endl;
		std::cin >> n;
	}
	return 0;
}