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

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++)

int item[111];
vector<int> cg[111];

int saiki(int a){
	if(cg[a].size()==0)return item[a];
	
	int sum=0;
	rep(i, cg[a].size()){
		sum+=saiki(cg[a][i]);
	}
	
	if(sum<item[a])return sum;
	return item[a];
}

int main(){
	while(1){
		int n;
		cin>>n;
		if(n==0)break;
		
		rep(i,111){
			item[i]=0;
			cg[i].clear();
		}
		map<string,int> N;
		
		rep(i,n){
			string a;int b;
			cin>>a>>b;
			
			N[a]=i;
			item[N[a]]=b;
		}
		
		int T;
		cin>>T;
		
		rep(i,T){
			string a;int b;
			cin>>a>>b;
			
			rep(j,b){
				string c;
				cin>>c;
				cg[N[a]].push_back(N[c]);
			}
		}
		
		string d;
		cin>>d;
		
		printf("%d\n",saiki(N[d]));
	}
}