#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	int n,m,i,j,k,x;
	map<string,int> cost;
	typedef vector<string> VS;
	typedef pair<string,VS>Recipe;
	vector<Recipe> recipe;	
	string s;
	while(cin>>n,n)
	{
		cost.clear();
		recipe.clear();

		for(i=0;i<n;++i) cin>>s>>x, cost[s]=x;
		cin>>m;
		for(i=0;i<m;++i)
		{
			cin>>s>>x;
			recipe.push_back( Recipe(s,VS()) );
			for(j=0;j<x;++j)
			{
				cin>>s;
				recipe.back().second.push_back(s);
			}
		}

		for(i=0;i<m;++i)
			for(j=0;j<m;++j)
		{
			x=0;
			for(k=0;k<recipe[j].second.size();++k)
				x += cost[ recipe[j].second[k] ];
			cost[recipe[j].first] = min(cost[recipe[j].first], x);
		}

		cin>>s;
		cout<<cost[s]<<endl;

	}
	return 0;
}