#include<iostream>
#include<map>
#include<string>

int main(){
	int n;
	while (std::cin>>n,n){
		std::cin >> n;
		std::map<std::string, int>items;
		for (int i = 0; i < n; i++){
			std::string str;
			int a;
			std::cin >> str >> a;
			items.insert(std::make_pair(str, a));
		}
		int renkinn;
		std::cin >> renkinn;
		for (int i = 0; i < renkinn; i++){
			std::string item;
			int sum = 0, a;
			std::cin >> item >> a;
			for (int j = 0; j < a; j++){
				std::string b;
				std::cin >> b;
				sum += items[b];
			}
			if (sum < items[item])items[item] = sum;
		}
		std::string ans;
		std::cin >> ans;
		std::cout << items[ans] << std::endl;
	}
	return 0;
}