#include <bits/stdc++.h>
using namespace std;

class Item{
public:
	string name;
	int cost;
	vector< Item* > recipe;
	
	Item(string _name, int _cost){
		name = _name;
		cost = _cost;
	}
	Item(){ name = ""; cost = 0; }
};

int Run(Item &item){
	int cost = 0;
	
	for(vector< Item* >::iterator it = item.recipe.begin(); it != item.recipe.end(); ++it){
		cost += Run( (**it) );
	}
	
	if(!item.recipe.empty()){
		return min(item.cost, cost);
	}
	return item.cost;
}

int main(){
	int n, m, p, k;
	string i, o, q, item;
	map< string, Item > mp;
	
	while(cin >> n, n){
		mp.clear();
		for(int j = 0; j < n; ++j){
			cin >> i >> p;
			mp.insert(make_pair< string, Item >(i, Item(i, p)));
		}
		cin >> m;
		
		for(int i = 0; i < m; ++i){
			cin >> o >> k;
			for(int j = 0; j < k; ++j){
				cin >> q;
				mp[o].recipe.push_back(&mp[q]);
			}
		}
		cin >> item;
		
		cout << Run(mp[item]) << endl;
	}
	
	return 0;
}