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

#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long

int calc(string name,map<string,int> data,map<string,vector<string> > recipe)
{
    if (recipe.find(name) != recipe.end())
    {
	int tmp=0;
	rep(i,recipe[name].size())
	{
	    tmp+=calc(recipe[name][i],data,recipe);
	}
	return min(tmp,data[name]);
    } else {
	return data[name];
    }
}

int main()
{
    while(1)
    {
	string name,tmp;
	int price,n,m;
	map<string,int> d;
	map<string,vector<string> > recipe;
	cin >> n;
	if (n==0)break;
	rep(i,n)
	{
	    cin >> name >> price;
	    d[name]=price;
	}
	cin >> m;
	rep(i,m)
	{
	    vector<string> v;
	    cin >> name >> n;
	    rep(j,n)
	    {
		cin >> tmp;
		v.push_back(tmp);
	    }
	    recipe[name] = v;
	}
	cin >> name;
	cout << calc(name,d,recipe) << endl;
    }
    return 0;
}