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

#define INF (10000000)
#define MIN(x, y) ((x) < (y) ? (x) : (y))

using namespace std;

map<string, int> ItemList;
map<string, list<string> > WroughtList;

int GetMinCost(string& butt)
{
	int wrought_cost;
	map<string, list<string> >::iterator it;
	map<string, int>::iterator mit;
	
	it = WroughtList.find(butt);
	
	if (it != WroughtList.end()){
		wrought_cost = 0;
		for (list<string>::iterator lit = it->second.begin(); lit != it->second.end(); lit++){
			wrought_cost += GetMinCost(*lit);
		}
	}
	else {
		wrought_cost = INF;
	}
	
	mit = ItemList.find(butt);
	
	return (MIN(wrought_cost, mit->second));
}

int main(void)
{
	int ItemCnt;
	int WroughtCnt;
	string sarch_item;
	pair<string, int> add;
	pair<string, list<string> > wadd;
	
	while (1){
		ItemList.clear();
		WroughtList.clear();
		cin >> ItemCnt;
		if (ItemCnt == 0) break;
		for (int i = 0; i < ItemCnt; i++){
			cin >> add.first >> add.second;
			ItemList.insert(add);
		}
		cin >> WroughtCnt;
		for (int i = 0; i < WroughtCnt; i++){
			int cnt;
			
			cin >> wadd.first >> cnt;
			wadd.second.clear();
			for (int i = 0; i < cnt; i++){
				string name;
				
				cin >> name;
				wadd.second.push_back(name);
			}
			WroughtList.insert(wadd);
		}
		
		cin >> sarch_item;
		
		cout << GetMinCost(sarch_item) << endl;
		
	}
	
	return (0);
}