#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>

using namespace std;

map<string, int> item;
map<string, vector<string>> recipe;

int min_score(string tage) {
	if (recipe[tage].size() == 0)return item[tage];

	int sum = 0;
	for (int i = 0; i < recipe[tage].size(); i++) sum += min_score(recipe[tage][i]);

	return min(sum, item[tage]);
}

int main() {
	int n;
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			string tmp; cin >> tmp;
			cin >> item[tmp];
		}
		int recipe_n; cin >> recipe_n;
		for (int i = 0; i < recipe_n; i++) {
			string targetItem; int targetItemRecipe;
			cin >> targetItem >> targetItemRecipe;
			for (int i = 0; i < targetItemRecipe; i++) {
				string tmp;
				cin >> tmp;
				recipe[targetItem].push_back(tmp);
			}
		}

		string tage;
		cin >> tage;
		cout << min_score(tage) << endl;

		item.clear();
		recipe.clear();
	}
}