#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

struct item{
	int p;
	int memo;
	vector<item*> q;
	
	item(): memo(-1) {}
};

map<string,item> table;

int getcost(item &it){
	if(it.memo == -1){
		it.memo = it.p;
		if(!it.q.empty()){
			int s = 0;
			for(int i = 0; i < it.q.size(); ++i){
				s += getcost(*it.q[i]);
			}
			it.memo = min(it.p, s);
		}
	}
	return it.memo;
}

int main(){
	int n, m, p, k;
	string s;
	while(cin >> n, n){
		table.clear();
		for(int i = 0; i < n; ++i){
			cin >> s >> p;
			table[s].p = p;
		}
		cin >> m;
		for(int i = 0; i < m; ++i){
			cin >> s >> k;
			vector<item*> &v = table[s].q;
			v.resize(k);
			for(int j = 0; j < k; ++j){
				cin >> s;
				v[j] = &table[s];
			}
		}

		cin >> s;
		cout << getcost(table[s]) << '\n';
	}
}