#include<iostream>
#include<list>
#include<string>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define foreach(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
using namespace std;
int main(){
	int n,m,k,f,c; string b1,b2;
	while(cin>>n,n){
		map<string,int> cost;
		map<string,list<string> > recipe;
		rep(i,n)cin>>b1>>c,cost[b1]=c;
		cin>>m; rep(i,m){
			cin>>b1>>k;
			list<string> r;
			rep(j,k)cin>>b2,r.push_back(b2);
			recipe[b1]=r;
		}
		f=0; __typeof(recipe.begin()) p;
		while(!f){
			f=1;
			foreach(i,cost)if((p=recipe.find(i->first))!=recipe.end()){
				c=0;
				foreach(j,p->second)c+=cost[*j];
				if(c<i->second)i->second=c,f=0;
			}
		}
		cin>>b1;
		cout<<cost[b1]<<endl;
	}
	return 0;
}