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

struct recipe{
	string n;
	vector<string> vs;
};

int main(){
	int n, m;
	int cost;
	string name;
	while(cin >> n){
		if(n == 0) break;
		map<string, int> costs;
		for(int i = 0; i < n; i++){
			cin >> name >> cost;
			costs[name] = cost;
		}
		cin >> m;
		vector<recipe> vr;
		for(int i = 0; i < m; i++){
			recipe r;
			int j;
			cin >> r.n;
			cin >> j;
			for(int k = 0; k < j; k++){
				cin >> name;
				r.vs.push_back(name);
			}
			vr.push_back(r);
		}
		for(int i = 0; i < n; i++){
			for(vector<recipe>::iterator iter = vr.begin(); 
				iter != vr.end(); iter++)
			{
				int cost = 0;
				for(vector<string>::iterator it = (*iter).vs.begin();
					it != (*iter).vs.end(); it++)
				{
					cost += costs[*it];
				}
				if(cost < costs[(*iter).n])
					costs[(*iter).n] = cost;
			}
		}
		cin >> name;
		cout << costs[name] << endl;
	}
	return 0;
}