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

using namespace std;

map<string,int> price,cost;
map< string,vector<string> > recipe;

int calcMinCost(string item)
{
	if(cost[item]!=-1)	return cost[item];

	vector<string> rcp=recipe[item];
	if(rcp.size()==0)	return cost[item]=price[item];

	int syncost=0;
	for(int i=0;i<rcp.size();i++)	syncost+=calcMinCost(rcp[i]);
	return cost[item]=min(price[item],syncost);
}

int main()
{
	for(int n;cin>>n,n;){
		for(int i=0;i<n;i++){
			string item;
			int p;	cin>>item>>p;
			price[item]=p;
			cost[item]=-1;
		}

		int m;	cin>>m;
		for(int i=0;i<m;i++){
			string item;
			int k;	cin>>item>>k;
			vector<string> material(k);
			for(int j=0;j<k;j++)	cin>>material[j];
			recipe[item]=material;
		}

		string tar;	cin>>tar;
		cout<<calcMinCost(tar)<<endl;
	}

	return 0;
}