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

map<string,int> price;
map<string,vector<string> > mix;

int res(string s){
	if(!mix.count(s))return price[s];
	int ans=0;
	for(int i=0;i<mix[s].size();i++){
		ans+=res(mix[s][i]);
	}
	if(price[s]>ans)price[s]=ans;
	return price[s];
}

int main(){
	int n,b,c;
	string a,d;
	while(cin>>n,n){
		price.clear();mix.clear();
		for(int i=0;i<n;i++){
			cin>>a>>b;
			price[a]=b;
		}
		cin>>c;
		for(int i=0;i<c;i++){
			cin>>a>>b;
			for(int i=0;i<b;i++){
				cin>>d;
				mix[a].push_back(d);
			}
		}
		cin>>d;
		cout<<res(d)<<endl;
	}
}