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

using namespace std;

map<string,int> minCosts;
map<string,int> value;
map<string,vector<string> > gousei;

int resolveMinCost(string name){
	if(minCosts.find(name)!=minCosts.end())
		return minCosts[name];

	int cost=value[name];
	if(gousei.find(name)!=gousei.end()){
		int sumCost=0;
		for(int i = 0; i < gousei[name].size(); i++){
			sumCost+=resolveMinCost(gousei[name][i]);
		}
		cost=min(cost,sumCost);
	}

	minCosts[name]=cost;
	return cost;
}

int main(){

	int n;
	while(cin>>n&&n!=0){
		for(int i =0; i < n; i++){
			string str;
			cin>>str;
			int val;
			cin>>val;
			value[str]=val;
		}
		int m;
		cin>>m;
		for(int i = 0; i < m; i++){
			string name;
			int k;
			cin>>name;
			cin>>k;
			vector<string> v;
			for(int j = 0; j < k; j++){
				string str;
				cin>>str;
				v.push_back(str);
			}
			gousei[name]=v;
		}
		string item;
		cin>>item;

		cout<<resolveMinCost(item)<<endl;

		gousei.clear();
		value.clear();
		minCosts.clear();
	}
	
	return 0;
}