#include <stdio.h>
#include <string.h>

int resipi[101][101],item_price[101],visit[101];
char item_name[101][101];

int func(int num){
	int i,j;
	int ans=0;
	if(resipi[num][0]==0)
		return item_price[num];
	
	for(i=1;i<=resipi[num][0];i++){
		
		if(visit[resipi[num][i]]==1){
			ans+=item_price[resipi[num][i]];
		}
		else if(resipi[resipi[num][i]][0]==0){
			visit[resipi[num][i]]=1;
			ans+=item_price[resipi[num][i]];
		}
		else{
			visit[resipi[num][i]]=1;
			ans+=func(resipi[num][i]);
		}
	}
	if(ans<item_price[num])item_price[num]=ans;
	return item_price[num];
}


int main(void){
	int n;
	while(scanf("%d",&n),n){
		int i,j,l,m,k,t;
		char name[101];
		
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%s%d",item_name[i],&item_price[i]);
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			scanf("%s%d",name,&k);
			for(j=0;j<n;j++){
				if(!(strcmp(item_name[j],name))){
					t=j;
					resipi[t][0]=k;
				}
			}
			for(j=0;j<k;j++){
				scanf("%s",name);
				for(l=0;l<n;l++){
					if(!(strcmp(item_name[l],name))){
						resipi[t][j+1]=l;
						break;
					}
				}
			}
		}
		scanf("%s",name);
		for(i=0;i<n;i++){
			if(!(strcmp(item_name[i],name))){
				break;
			}
		}
		printf("%d\n",func(i));
		
		for(i=0;i<101;i++){
			resipi[i][0]=0;
			visit[i]=0;
		}
	}
	return 0;
}