#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<stdlib.h>

void listen_price(std::map<std::string,int> &);
void listen_recipe(std::map<std::string,std::vector<std::string> > &);
void listen_choicedItem(std::string &);
void echo_bestPrice(std::string &,std::map<std::string,int> &,std::map<std::string,std::vector<std::string> >&);
int  calc_bestPrice(std::string &,std::map<std::string,int> &,std::map<std::string,std::vector<std::string> >&);

void wroughtGoldMaster();

//-------------------main------------------------------

int main()
{
	for(;1;)
	{
		wroughtGoldMaster();//終了処理は関数内のexitで行われる
	}
	return 0;
}
void wroughtGoldMaster()
{
	std::map<std::string,int> price;
	listen_price(price);
	
	std::map<std::string,std::vector<std::string> > recipe;//本来はmultimapを使うべきかもしれないが練習のため
	listen_recipe(recipe);
	
	std::string choicedItem;
	listen_choicedItem(choicedItem);
	echo_bestPrice(choicedItem,price,recipe);
}

//----------------------------------------------------

void listen_price(std::map<std::string,int> &price)
{
	using namespace std;
	
	int kind_num;
	cin>>kind_num;
	if(!kind_num)
	{
		exit(0);//入力の終わり
	}
	
	string tmp_string;
	int tmp_value;
	for(int roop=0;roop<kind_num;roop++)
	{
		cin>>tmp_string>>tmp_value;
		price[tmp_string]=tmp_value;
	}
}

void listen_recipe(std::map<std::string,std::vector<std::string> >&recipe)
{
	using namespace std;
	int kind_num;
	cin>>kind_num;
	
	string tmp_string;
	string tmp_string_forItem;
	int itemCount_num;
	for(int roop=0;roop<kind_num;roop++)
	{
		cin>>tmp_string>>itemCount_num;
		for(int itemroop=0;itemroop<itemCount_num;itemroop++)
		{
			cin>>tmp_string_forItem;
			recipe[tmp_string].push_back(tmp_string_forItem);
		}
	}
}

void listen_choicedItem(std::string &choicedItem)
{
	using namespace std;
	
	cin>>choicedItem;
}

void echo_bestPrice(std::string &choicedItem,std::map<std::string,int> &price,std::map<std::string,std::vector<std::string> >&recipe)
{
	using namespace std;
	
	cout<<calc_bestPrice(choicedItem,price,recipe)<<endl;//calc_bestPrice:再帰的にレシピを探索して各品目の最安コストを探索するための関数
}

int calc_bestPrice(std::string &item,std::map<std::string,int> &price,std::map<std::string,std::vector<std::string> >&recipe)
{
	using namespace std;
	
	vector<string>::iterator itr;
	int sum_compCost;
	if(recipe.find(item)==recipe.end())
	{
		return price[item];
	}
	else
	{
		sum_compCost=0;
		for(itr=recipe[item].begin();itr<recipe[item].end();++itr)
		{
			sum_compCost+=calc_bestPrice(*itr,price,recipe);
		}
	}
	
	if(sum_compCost<price[item])
	{
		return sum_compCost;
	}
	else
	{
		return price[item];
	}
}