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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

typedef long long ll;

vector<string> split(string &str,char delim)
{
	vector<string> res;
	size_t current = 0,found;
	while((found = str.find_first_of(delim,current)) != string::npos)
	{
		res.push_back(string(str,current,found  - current));
		current = found + 1;
	}
	res.push_back(string(str,current,str.size() - current));
	return res;
}
int main()
{
	int n,m;
	while(cin >> n && n)
	{
		map<string,ll> ma;
		
		rep(i,n)
		{
			string s;
			ll temp;
			cin >> s >> temp;
			ma[s] = temp;
		}

		cin >> m;
		vector<vector<string> > v(m);
		
		rep(i,m)
		{
			string s1;
			ll t;
			cin >> s1 >> t;
			
			v[i].push_back(s1);

			rep(j,t)
			{
				cin >> s1;
				v[i].push_back(s1);
			}
		}
		
		rep(i,m)
		{
			rep(j,m)
			{
				ll t = 0;
				REP(k,1,v[j].size())
				{
					t += ma[v[j][k]];
				}
				ma[v[j][0]] = min(ma[v[j][0]],t);
			}
		}
		
		string ans;
		cin >> ans;

		cout << ma[ans] << endl;
	}
	return 0;
}