#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
int keisan(char[100]);
struct SS{
	char name[100];
	int cnt;
	char zairyo[100][100];
};
struct AAA{
	char na[100];
	int cost;
};
SS recipe[100];
AAA IP[100];
int n,m;
char target[100];
int main()
{
	while(1){
		scanf("%d",&n);
		if(n == 0) break;
		for(int i=0;i<n;i++) 
			scanf("%s %d",IP[i].na,&IP[i].cost);
		scanf("%d",&m);////n
		for(int i=0;i<m;i++){//n
			scanf("%s %d", recipe[i].name, &recipe[i].cnt);
			for(int j=0; j<recipe[i].cnt; j++){
				scanf("%s",recipe[i].zairyo[j]);
			}
		}
		scanf("%s",target);
	//////////////////////////
	/*printf("%d\n",n);
	for(int i=0;i<n;i++) 
		printf("%s %d\n",IP[i].na,IP[i].cost);
	printf("%d\n",m);
	for(int i=0;i<m;i++){//n
		printf("%s %d ", recipe[i].name, recipe[i].cnt);
		for(int j=0; j<recipe[i].cnt; j++){
			printf("%s ",recipe[i].zairyo[j]);
		}
		printf("\n");
	}
	printf("%s\n",target);*/
	/////////////////////////
		printf("%d\n",keisan(target));
	}
	return 0;
}

int keisan(char name[100]/*,int money*/)
{
	int money = 0;//
	for(int i=0;i<m;i++){
		//printf("Money=%d\n",money);
		if(strstr(name,recipe[i].name) != NULL){//??¬?????????????????????????????????
			////printf("|%s|\n",recipe[i].name);
			for(int j=0;j<recipe[i].cnt;j++){
				money = money + keisan(recipe[i].zairyo[j]/*,money*/);
				//printf("%d Money=%d\n",j,money);
			}
			return money;
		}
	}
	for(int i=0;i<n;i++){
		if(strstr(name,IP[i].na) != NULL){
			////printf("%s %d\n",IP[i].na,IP[i].cost);
			return IP[i].cost;
		}
	}
}