#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <string>

using namespace std;

typedef pair<string, int> psi;

int main(){
	while(true){
		int n, m;
		scanf("%d", &n);
		if(n == 0){ break; }
		map<string, int> items;
		for(int i = 0; i < n; i++){
			string name;
			int cost;
			cin >> name >> cost;
			items.insert(psi(name, cost));
		}
		map< string, set<string> > materials;
		queue<string> q;
		scanf("%d", &m);
		for(int i = 0; i < m; i++){
			string item;
			int k, cost = 0;
			set<string> material;
			cin >> item >> k;
			for(int j = 0; j < k; j++){
				string name;
				cin >> name;
				material.insert(name);
				cost += items[name];
			}
			materials.insert(pair< string, set<string> >(item, material));
			if(cost < items[item]){
				items[item] = cost;
				q.push(item);
			}
		}
		while(!q.empty()){
			string item = q.front();
			q.pop();
			map< string, set<string> >::iterator it = materials.begin();
			while(it != materials.end()){
				if((*it).second.find(item) != (*it).second.end()){
					int cost = 0;
					set<string>::iterator jt = (*it).second.begin();
					while(jt != (*it).second.end()){
						cost += items[*jt];
						jt++;
					}
					if(cost < items[(*it).first]){
						items[(*it).first] = cost;
						q.push((*it).first);
					}
				}
				it++;
			}
		}
		string query;
		cin >> query;
		printf("%d\n", items[query]);
	}
	return 0;
}