#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
int tot, maxlt;
unordered_map<string,int> M;
double C[505][105];
unordered_map<int,double> dp[505][505];
string toCol[305];
inline int getid(const string &s) {
	auto f=M.find(s);
	if(f==M.end())	return toCol[tot]=s, M[s]=tot++;
	return f->second;
}
inline int dcmp(double x) {
	return fabs(x) < 1e-7 ? 0 : x<0 ? -1 : 1;
}
struct Edge {
    int v,g,nxt;
}E[205];
int H[305], ecnt;
inline void addEdge(int u,int v,int g) {
    E[ecnt]=(Edge){v,g,H[u]}, H[u]=ecnt++;
}
inline double get(int L,int R,int c) {
    auto f=dp[L][R].find(c);
    return f==dp[L][R].end() ? -1e10 : dp[L][R][c];    
}
inline void update(unordered_map<int,double> &dp,int g,double r1,double r2) {
    auto f=dp.find(g);
    if(f==dp.end() && r1+r2>-1e10)     dp[g]=r1+r2;
    else  f->second=max(f->second, r1+r2);
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("A.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    // ios::sync_with_stdio(false);		cin.tie(0);    
	int rulecnt;	scanf("%d", &rulecnt);    
	vector<pair<pair<string,string>,string> >abc;
	CLR(H,-1);
	rep(i,rulecnt) {
		static char aa[50],bb[50],cc[50];
		scanf("%s%s%s",aa,bb,cc);
        int a=getid(aa), b=getid(bb), c=getid(cc);
        addEdge(a,b,c), addEdge(b,a,c);
	}				
	int T;	scanf("%d", &T);
	char col[50];    
	while(T--) {
		int n;	scanf("%d", &n);		
		rep(i,n) {
            rep(j,tot)  C[i][j]=-1e10;
			scanf("%s",col);
			while(strcmp(col,"END")) {
				double p;	scanf("%lf", &p);
				C[i][getid(col)]=log(p);
				scanf("%s",col);
			}			
		}		        
        rep(i,n)
        REP(j,i,n)      dp[i][j].clear();
		rep(i,n)
        rep(j,tot) if(C[i][j]>-1e10)      dp[i][i][j]=C[i][j];
        FOR(len,2,n)
        FOR(L,0,n-len) {
            int R=L+len-1;
            REP(i,L,R) {                
                for(auto cp:dp[L][i]) {
                    int c1=cp.first;
                    double p1=get(L,i,c1);                    
                    for(int e=H[c1];~e;e=E[e].nxt) if(dp[i+1][R].count(E[e].v)){
                        int c2=E[e].v, g=E[e].g;                        
                        update(dp[L][R], g, p1, get(i+1,R,c2));                        
                    }
                }
            }
        }
        double res=-1e20;   int resi=-1;
		for(auto &ii:dp[0][n-1]) {
			double t=ii.second;			
			if(t>res)
				res=t, resi=ii.first;
		}				
		puts(~resi ? toCol[resi].c_str() : "GAMEOVER");        
	}
    return 0;
}