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

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)



map<string,int> sina;
vector<int> haigo[100];
int val[100];

int saiki(int a){
	if(haigo[a].size()==0)return val[a];
	
	int sum=0;
	for(int i=0;i<haigo[a].size();i++){
		sum += saiki(haigo[a][i]);
	}
	
	return min(sum,val[a]);
}

int main(){
	while(1){
		int n;
		cin>>n;
		
		if(n==0)break;
		
		sina.clear();
		
		rep(i,n){
			string a;
			int b;
			
			cin>>a>>b;
			sina[a]=i;
			val[i]=b;
		}
		
		int k;
		cin>>k;
		
		rep(i,100)haigo[i].clear();
		
		rep(i,k){
			string a;
			int b;
			cin>>a>>b;
			
			rep(j,b){
				string c;
				cin>>c;
				
				haigo[sina[a]].push_back(sina[c]);
			}
		}
		
		string d;
		cin>>d;
		
		printf("%d\n",saiki(sina[d]));
	}
}