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

using namespace std;

#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

map<string, int> price;
map<string, vector<string> > resipi;

map<string, int> memo;

int solve(string str)
{
	if(memo.count(str))
		return memo[str];
	
	memo[str] = -1;
	
	if(!resipi.count(str))
		return memo[str] = price[str];
	
	int cost = 0;
	rep(i, resipi[str].size()){
		int a = solve(resipi[str][i]);
		if(a == -1)
			return memo[str] = price[str];
		
		cost += a;
	}
	
	printf("%s is %d\n", str.c_str(), min(price[str], cost));
	return memo[str] = min(price[str], cost);
}

int main()
{
	int n;
	while(scanf("%d", &n), n){
		price.clear();
		resipi.clear();
		memo.clear();
		
		rep(i, n){
			char name[128];
			int p;
			scanf("%s%d", name, &p);
			price[string(name)] = p;
		}
		
		int m;
		scanf("%d", &m);
		rep(i, m){
			char name[128];
			int k;
			scanf("%s%d", name, &k);
			rep(j, k){
				char factor[128];
				scanf("%s", factor);
				resipi[string(name)].push_back(string(factor));
			}
		}
		
		char name[128];
		scanf("%s", name);
		printf("%d\n", solve(string(name)));
	}
	
	return 0;
}