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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FFOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,b) FOR(i,0,b)
#define RREP(i,b) FFOR(i,1,b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut,pr> ppr;
typedef priority_queue<pr,Vpr, greater<pr> > PQ;
const int SIZE=5+3*1e+4;
const ut INF=1<<30;
const ld eps=1e-6;
const LL p=7+1e+9;

int N, M;
map<string, int> items;
map<string, vector<string>> recipe;

int dfs(string s){
	int res = 0;
	if(recipe[s].empty()){
		return items[s];
	}
	else{
		REP(i,recipe[s].size()){
			res += min(items[recipe[s][i]], dfs(recipe[s][i]));
		}
		res = min(res, items[s]);
	}
	return res;
}

int main(){
	while(cin >> N && N){
		string s;
		int n;
		REP(i,N){
			cin >> s >> n;
			items[s] = n;
		}
		cin >> M;
		string c, q;
		int k;
		REP(i,M){
			cin >> c >> k;
			vector<string> tmp;
			REP(j,k){
				cin >> q;
				tmp.push_back(q);
			}
			recipe[c] = tmp;
		}
		string goal;
		cin >> goal;
		cout << dfs(goal) << endl;
	}
}