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

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

struct data_t {
	string name;
	double eff;

	bool operator<(const data_t& right) const {
		if (eff == right.eff) {
			return ( name < right.name ) || ( name == right.name );
		}
		else {
			return ( eff > right.eff );
		}
	}
};

int main(void) {
	int N;
	cin >> N;

	while ( N != 0 ) {
		vector<data_t> LS(N);
		vector<int> T(N,0);
		int p,temp,d,e,f,s,m;
		for (int i = 0; i < N; i++) {
			cin >> LS[i].name;
			cin >> p;
			for (int j = 0; j < 3; j++) {
				cin >> temp;
				T[i] += temp;
			}
			cin >> d >> e >> f >> s >> m;
			T[i] += m * (d+e);
			LS[i].eff = (double)((m*f*s)-p)/T[i];
		}

		std::sort(LS.begin(), LS.end());

		for (int i = 0; i < N; i++) {
			cout << LS[i].name << endl;
		}
		cout << "#" << endl;
		cin >> N;
	}

	return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
	int u[150][150],p,v[150][150],xg,yg,q,dx[]={1,0,-1,0},dy[]={0,1,0,-1},w,h;
void ret(int x,int y){
	int a,b,i;
	v[y][x]=1;
	if(xg==x&&yg==y)
		p++;
	else
	for(i=0;i<4;i++){
		a=x+dx[i];
		b=y+dy[i];
		if(a<1||a>w||b<1||b>h||v[b][a]==1)
			continue;
		else if(u[b][a]==q)
			ret(a,b);
	}
}
int main(){
	int xs,ys,c,d,x,y,n;
	while(cin>>w>>h,w||h){
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
			v[i][j]=0;
		scanf("%d %d",&xs,&ys);
		scanf("%d %d",&xg,&yg);
		scanf("%d",&n);
		for(int k=0;k<n;k++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			if(d==0)
				for(int i=0;i<2;i++)
				for(int j=0;j<4;j++)
				u[i+y][j+x]=c;
			else 
				for(int i=0;i<4;i++)
				for(int j=0;j<2;j++)
				u[i+y][j+x]=c;
		}
		q=u[ys][xs];
		p=0;
		ret(xs,ys);
		if(p>0)
			printf("OK\n");
		else printf("NG\n");
	}
	return 0;
}