#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

struct Info{
	Info(){
		cost = 0;
	}
	char name[101];
	int cost;
};

struct Recipe{
	Recipe(){
		index = num = 0;
		used = false;
	}

	void setNUM(int arg_num){
		component = new int[arg_num];
	}

	void push(int id){
		component[index++] = id;
	}

	int* component;
	int menu,index;
	int num;
	bool used;
};


bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

void func(int n){

	Info info[n];
	int checkTable[n];
	for(int i = 0; i < n; i++)checkTable[i] = 0; //0:fix 1:not

	for(int i = 0; i < n; i++){
		scanf("%s %d",info[i].name,&info[i].cost);
	}

	int m;
	scanf("%d",&m);
	char tmp[101];
	Recipe recipes[m];

	for(int i = 0; i < m; i++){
		scanf("%s %d",tmp,&recipes[i].num);
		for(int k = 0; k < n; k++){
			if(strCmp(tmp,info[k].name)){
				recipes[i].menu = k;
				break;
			}
		}

		checkTable[recipes[i].menu] = 1;

		recipes[i].setNUM(recipes[i].num);

		for(int k = 0; k < recipes[i].num; k++){
			scanf("%s",tmp);
			for(int p = 0; p < n; p++){
				if(strCmp(tmp,info[p].name)){
					recipes[i].push(p);
					break;
				}
			}
		}
	}

	int count,sum;
	bool FLG;

	while(true){

		count = 0;
		for(int i = 0; i < n; i++){
			if(checkTable[i] == 1){
				count++;
			}
		}
		if(count == 0)break;

		for(int i = 0; i < m; i++){
			if(recipes[i].used == false){
				FLG = true;
				sum = 0;

				for(int k = 0; k < recipes[i].num; k++){
					sum += info[recipes[i].component[k]].cost;
					if(checkTable[recipes[i].component[k]] == 1){
						FLG = false;
						break;
					}
				}
				if(FLG){
					info[recipes[i].menu].cost = min(info[recipes[i].menu].cost,sum);
					recipes[i].used = true;
					checkTable[recipes[i].menu] = 0;
				}
			}
		}
	}

	char to_make[101];
	scanf("%s",to_make);

	for(int i = 0; i < n; i++){
		if(strCmp(info[i].name,to_make)){
			printf("%d\n",info[i].cost);
			break;
		}
	}
}

int main(){

	int n;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		func(n);

	}

	return 0;
}