#include<iostream>
#include<string>
#include<vector>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
map<string,vector<string> > mp;
map<string,int> vec;
int func(string s){
	int sum=0;
	if(mp[s].size()==0)
	sum=100000000;
	else
	rep(i,(int)mp[s].size()){
		sum+=func(mp[s][i]);
	}
	return min(vec[s],sum);
}
int main(){
	int n,x;
	string s,a;
	while(cin>>n,n){
		rep(i,n){
			cin>>s>>x;
			vec[s]=x;
		}
		cin>>n;
		while(n--){
			cin>>s;
			cin>>x;
			rep(i,x){
				cin>>a;
				mp[s].push_back(a);
			}
		}
		cin>>s;
		cout<<func(s)<<endl;
		mp.clear();
		vec.clear();
	}
	return 0;
}