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

int main() {
	int n;
	while(cin >> n, n) {
		int item;
		string name;
		map<string, int> items;
		for(int i = 0; i < n; ++i) {
			cin >> name >> item;
			items.insert(map<string, int>::value_type(name, item));
		}

		int m;
		cin >> m;
		for(int i = 0; i < m; ++i) {
			int k, sum = 0;
			string o;
			cin >> o >> k;
			for(; k > 0; --k) {
				string q;
				cin >> q;
				sum += items[q];
			}
			items[o] = min(items[o], sum);
		}

		string ans;
		cin >> ans;
		cout << items[ans] << endl;
	}
}