#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

ll n;
map<string, ll> msi;
map<string, vector<string>> msvs;
ll m;
string t;

ll solve(string s){
	if (msvs.find(s) == msvs.end()){
		return msi[s];
	}
	else{
		ll res = 0;
		for (vector<string>::iterator it = msvs[s].begin(); it != msvs[s].end(); it++){
			res += solve(*it);
		}
		return min(msi[s], res);
	}
}

int main(){
	while (cin >> n&&n){
		msi.clear();
		msvs.clear();
		REP(i, n){
			string s;
			ll p;
			cin >> s >> p;
			msi[s] = p;
		}
		cin >> m;
		REP(i, m){
			string o;
			ll k;
			cin >> o >> k;
			REP(j, k){
				string q;
				cin >> q;
				msvs[o].push_back(q);
			}
		}
		cin >> t;
		cout << solve(t) << endl;
	}
}