#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;
map<string,int> dic;
map<string,bool> isp;
map< string, vector<string> > gm;

int solve(string s){
    int r = dic[s];
    int t = 0;
    if(gm[s].size()){
	REP(i,gm[s].size()){
	    t += solve(gm[s][i]);
	}
    }else{
	return dic[s];
    }
    r = min(r,t);
    return r;
}

int main(){
    while(true){
	int n;
	cin >> n;
	if(n==0) break;
	REP(i,n){
	    string s;
	    int p;
	    cin >> s >> p;
	    dic[s] = p;
	    isp[s] = false;
	    gm[s] = vector<string>(0);
	}
	cin >> n;
	REP(i,n){
	    string s;
	    int m;
	    cin >> s >> m;
	    REP(j,m){
		string temp;
		cin >> temp;
		isp[temp] = true;
		gm[s].PB(temp);
	    }
	}
	string s;
	cin >> s;
	cout << solve(s) << endl;
    }
    return 0;
}