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

using namespace std;

map<string,int> item;

const int INF = 2000000000;

class Recipe{
public:
	string To;
	vector<string> From;
	int GetCost(){
		if(From.size() == 0){
			return INF;
		}
		int c = 0;
		vector<string>::iterator it;
		for(it = From.begin();it<From.end();it++){
			c += item[*it];
		}
		return c;
	}
};


map<string,Recipe> recipe;



int itemn;
int recipen;

int main(){
	int self;
	char str[256];
	while(scanf("%d",&itemn) != EOF){
		//if(itemn == 0){break;}
		//printf("CASE\n");
		item.clear();
		recipe.clear();
		//printf("ITEMS %d:\n",itemn);
		for(int i=0;i<itemn;i++){
			scanf("%s %d",str,&self);
			item[str] = self;
			//printf("%s	%d\n",str,self);
		}
		if(scanf("%d",&recipen) == EOF) break;
	
		for(int i=0;i<recipen;i++){
			Recipe r;
			scanf("%s %d",str,&self);
			r.To = str;
			for(int j=0;j<self;j++){
				scanf("%s",str);
				r.From.push_back(str);
			}
			recipe[r.To] = r;
		}
		scanf("%s",str);
		
		//printf("TARGET:%s\n",str);
		printf("%d\n" , item[str] < recipe[str].GetCost() ? item[str] : recipe[str].GetCost() );
	}
	return 0;
}