#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);
#define sintttt(i,j,k,m) scanf("%d%d%d%d",&i,&j,&k,&m);

#define INF 1010000000
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};


int cost[101];
vector<int> haigo[101];

int saiki(int n){
	if(haigo[n].size()==0){
		return cost[n];
	}
	
	int sum=0;
	rep(i,haigo[n].size()){
		sum+=saiki(haigo[n][i]);
	}
	return min(cost[n],sum);
}

int main(){
	while(1){
		int n;
		sint(n);
		if(n==0)break;
		
		rep(i,101){
			haigo[i].clear();
		}
		
		map<string,int> sina;
		rep(i,n){
			string str;
			int a;
			cin>>str>>a;
			sina[str]=i;
			cost[i]=a;
		}
		
		int m;
		sint(m);
		
		
		rep(i,m){
			string a;
			int b;
			cin>>a>>b;
			rep(j,b){
				string c;
				cin>>c;
				haigo[sina[a]].push_back(sina[c]);
			}
		}
		
		string toi;
		cin>>toi;
		
		printf("%d\n",saiki(sina[toi]));
	}
}