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

using namespace std;

vector<string> item;
vector<int> cost;
vector<vector<string> > list;

map<string,int> name;

int saiki(int n){
	int cont=0;
	
	if(list[n].empty()==1){
		return cost[n];
	}
	
	for(int i=0;i<list[n].size();i++){
		cont+=saiki(name[list[n][i]]);
	}
	
	return min(cont,cost[n]);
}
int main(){
	while(1){
		int n;
		cin>>n;
		if(n==0){
			break;
		}
		item.clear();
		cost.clear();
		list.clear();
		for(int i=0;i<n;i++){
			string a;
			int b;
			cin>>a>>b;
			item.push_back(a);
			cost.push_back(b);
			name[a]=i;
		}
		int m;
		cin>>m;
		for(int i=0;i<n;i++){
			vector<string> kari;
			list.push_back(kari);
		}
		for(int i=0;i<m;i++){
			string a;
			int b;
			cin>>a>>b;
			vector<string> kari;
			kari.clear();
			for(int j=0;j<b;j++){
				string c;
				cin>>c;
				kari.push_back(c);
			}
			list[name[a]]=kari;
		}
		
		string a;
		cin>>a;
		cout<<saiki(name[a])<<endl;;
	}
}