#include<iostream>
#include<map>

struct Recipe{
	int index, stuff[100], size;
};

int main(){
	int n;
	while(std::cin >> n, n){
		std::map<std::string, int> indexes;
		int items[100];//items[index]: index番目の値段
		for(int i=0;i<n;i++){
			std::string name;
			int price;
			std::cin >> name >> price;
			indexes[name] = i;
			items[i] = price;
		}
		Recipe r[100];//recipe[n][0]には材料の数

		int m;
		std::cin >> m;
		for(int i=0;i<m;i++){
			std::string name;
			int stuff_n;
			std::cin >> name >> stuff_n;

			r[i].size = stuff_n;
			r[i].index = indexes[name];

			for(int j=0;j<stuff_n;j++){
				std::string stuff;
				std::cin >> stuff;
				r[i].stuff[j] = indexes[stuff];
			}
		}

		while(true){
			bool update = false;
			for(int i=0;i<m;i++){
				int price = 0;
				for(int j=0;j<r[i].size;j++){
					price += items[r[i].stuff[j]];
				}
				if(price < items[r[i].index]){
					items[r[i].index] = price;
					update = true;
				}
			}
			if(!update)break;
		}

		std::string want;
		std::cin >> want;
		std::cout << items[indexes[want]] << std::endl;
	}
}