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

struct RECIPE{
	string o;
	int k;
	string q[100];
};

RECIPE recipe[100];
int n;
pair<string, int> ip[100];
int m;

int func(string order){

	int x, y;
	bool flg = false;
	for (int i = 0; i < m; i++){
		if (recipe[i].o == order){
			flg = true;
			x = i;
			break;
		}
	}
	for (int i = 0; i < n; i++){
		if (ip[i].first == order){
			y = i;
			break;
		}
	}
	int sum = 0;
	if (flg){
		for (int i = 0; i < recipe[x].k; i++){
			sum += func(recipe[x].q[i]);
		}
	}
	else{
		for (int i = 0; i < n; i++){
			if (ip[i].first == order)
				return ip[i].second;
		}
	}

	return min(sum, ip[y].second);
}

int main(void){

	while (cin >> n && n != 0){
		string order;

		for (int i = 0; i < n; i++)
			cin >> ip[i].first >> ip[i].second;
		cin >> m;
		for (int i = 0; i < m; i++){

			cin >> recipe[i].o >> recipe[i].k;
			for (int j = 0; j < recipe[i].k; j++)
				cin >> recipe[i].q[j];
		}

		cin >> order;
		cout << func(order) << endl;
	}
}