#include <iostream>
#include <string>
#include <vector>
#include <map>
#define int long long
using namespace std;

int n,p[100],cost[100];
vector<int> recipe[100];

int dfs(int v){
	int sum = 0;
	if(cost[v] != -1) return cost[v];
	if(recipe[v].size() == 0) return cost[v] = p[v];
	for(int i = 0;i < recipe[v].size();i++){
		sum += dfs(recipe[v][i]);
	}
	return cost[v] = min(p[v],sum);
}

signed main(){
	int m;
	string t;
	while(cin >> n,n){
		for(int i = 0;i < 100;i++) {
			recipe[i].clear();
			cost[i] = -1;
		}
		map<string,int> mp;
		for(int i = 0;i < n;i++){
			string s;
			cin >> s >> p[i];
			mp[s] = i;
		}
		cin >> m;
		for(int i = 0;i < m;i++){
			int k;
			string s,q;
			cin >> s >> k;
			for(int j = 0;j < k;j++){
				cin >> q;
				recipe[mp[s]].push_back(mp[q]);
			}
		}
		cin >> t;
		cout << dfs(mp[t]) << endl;
	}
	return 0;
}