#include<iostream>
#include<map>
#include<string>
#include<vector>
using namespace std;
map<string,int>price;
map<string,vector<string> >rec;
int R(string o){
	int m=0,i;
	if(rec[o].size()){
		for(i=0;i<rec[o].size();i++)
			m+=R(rec[o][i]);
	}else
		m=price[o];
	if(price[o]&&price[o]<m)
		m=price[o];
	return m;
}
int main(){
	int n,m;
	string s,t;
	for(;cin>>n,n;){
		price.clear();
		rec.clear();
		for(;n--;){
			cin>>s;
			cin>>price[s];
		}
		cin>>n;
		for(;n--;){
			cin>>s>>m;
			for(;m--;){
				cin>>t;
				rec[s].push_back(t);
			}
		}
		cin>>s;
		cout<<R(s)<<endl;
	}
	return 0;
}