#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main(){

	map<string, int> end;
	int m;
	int cost; string material;
	while (cin >> m, m){
		for (; m > 0; m--){
			cin >> material >> cost;
			end.insert(map<string, int>::value_type(material, cost));
		}
		cin >> m;
		for (; m > 0; m--){
			string sozai; int kaisuu; int sum = 0;
			cin >> sozai >> kaisuu;
			for (; kaisuu > 0; kaisuu--){
				cin >> material;
				sum += end[material];
			}
			if (sum < end[sozai]) end[sozai] = sum;
			sum = 0;
		}
		string in;
		cin >> in;
		cout << end[in] << endl;
	}
	return 0;
}