// 0201
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct item{
	string name;
	int cost;
};

void update(vector<item> &I, vector<vector<int> > recipe, int m){
	for(int i=0;i<m;i++){
		for(int j=0;j<recipe.size();j++){
			if(recipe[j].size() != 0){
				int co = 0;
				for(int k=0;k<recipe[j].size();k++)
					co += I[recipe[j][k]].cost;

				if(I[j].cost > co)
					I[j].cost = co;
			}
		}
	}
}

int main(){
	while(1){
		int n, p;
		string str;
		item it;
		vector<item> I;

		cin>>n;
		if(n == 0)
			break;

		for(int i=0;i<n;i++){
			cin>>str>>p;
			it.name = str;
			it.cost = p;
			I.push_back(it);
		}

		int m, k;
		vector<vector<int> > recipe;

		recipe.resize(n);

		cin>>m;
		for(int i=0;i<m;i++){
			int l;
			cin>>str;
			
			for(int j=0;j<n;j++)
				if(str == I[j].name)
					l = j;

			cin>>k;
			for(int j=0;j<k;j++){
				cin>>str;
				for(int o=0;o<n;o++)
					if(str == I[o].name)
						recipe[l].push_back(o);
			}
		}

		update(I, recipe, m);

		cin>>str;
		for(int i=0;i<n;i++)
			if(str == I[i].name)
				cout<<I[i].cost<<endl;
	}
	return 0;
}