#include <cstdio>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
char buf[128];
vector<string> items;
map<string, int> info;
map<string, vector<string> > recipe;
int main()
{
	while (true) {
		int n;
		scanf("%d", &n);
		if (n == 0) break;
		rep(i, n) {
			int c;
			scanf("%s %d", buf, &c);
			info[string(buf)] = c;
			items.push_back(string(buf));
		}
		int m;
		scanf("%d", &m);
		rep(i, m) {
			int k;
			scanf("%s %d", buf, &k);
			string item(buf);
			rep(j, k) {
				scanf("%s", buf);
				recipe[item].push_back(string(buf));
			}
		}
		scanf("%s", buf);
		string wish(buf);

		while (true) {
			bool f = true;
			rep(i, items.size()) {
				if (recipe.find(items[i]) != recipe.end()) {
					int c = 0;
					rep(j, recipe[items[i]].size())
						c += info[recipe[items[i]][j]];
					if (c < info[items[i]]) {
						f = false;
						info[items[i]] = c;
					}
				}
			}
			if (f) break;
		}
		printf("%d\n", info[wish]);
	}
	return 0;
}
					