#include <bits/stdc++.h>
using namespace std;

struct data {
  string l;
  int p, a, b, c, d, e, f, s, m;
};

int n;
vector<data> v;

bool ch(data l, data r);
void solve();
double calc(data now);

int main() {
  while(1) {
    cin >> n;
    if(n == 0) break;
    for(int i = 0; i < n; ++i) {
      data now;
      cin >> now.l >> now.p >> now.a >> now.b >> now.c >>
          now.d >> now.e >> now.f >> now.s >> now.m;
      v.push_back(now);
    }
    solve();
    for(int i = 0; i < n; ++i) cout << v[i].l << endl;
    cout << "#" << endl;
    v.erase(v.begin(), v.end());
  }
  return 0;
}

bool ch(data l, data r) {
  double nowl = calc(l), nowr = calc(r);
  if(nowl != nowr) return nowl > nowr;
  return l.l < r.l;
}

void solve() { sort(v.begin(), v.end(), ch); }

double calc(data now) {
  double sumt =
      now.a + now.b + now.c + (now.d + now.e) * now.m;
  return (now.s * now.f * now.m - now.p) / sumt;
}

#include<bits/stdc++.h>
using namespace std;
pair<int,int> pii;

int main() {
	int w,h,n;
	int xs,ys;
	int xg,yg;
	int c,d,x,y;
	int v[101][101];
	int u[101][101];
	int flag=0;
	int xx[4]={-1,0,1,0};
	int yy[4]={0,1,0,-1};
	int xxx,yyy;
while(cin>>w>>h,w||h){
	cin>>xs>>ys;
	cin>>xg>>yg;
	cin>>n;
	fill(v[0],v[101],0);
	fill(u[0],u[101],0);
	for (int i = 0; i < n; ++i) {
			cin>>c>>d>>x>>y;
			if(d==0){
				v[x][y]=c;	v[x+1][y]=c;	v[x+2][y]=c;	v[x+3][y]=c;
				v[x][y+1]=c;	v[x+1][y+1]=c;	v[x+2][y+1]=c;	v[x+3][y+1]=c;
			}
			if(d==1){
				v[x][y]=c;	v[x][y+1]=c;	v[x][y+2]=c;	v[x][y+3]=c;
				v[x+1][y]=c;	v[x+1][y+1]=c;	v[x+1][y+2]=c;	v[x+1][y+3]=c;
			}
	}
	queue<pair<int,int> > q;
	u[xs][ys]=1;
	int color=v[xs][ys];
	q.push(make_pair(xs,ys));
	while(!q.empty()){
		pii=q.front();q.pop();
		if(pii.first==xg&&pii.second==yg){flag=1;break;}
		for (int i = 0; i < 4; ++i) {
			xxx=xx[i]+pii.first;
			yyy=yy[i]+pii.second;
			if(xxx>=1&&xxx<=w&&yyy>=1&&yyy<=h&&u[xxx][yyy]!=1&&color==v[xxx][yyy]){
				u[xxx][yyy]=1;
				q.push(make_pair(xxx,yyy));
			}
		}
	}
	if(flag){cout<<"OK"<<endl;}
	else{cout<<"NG"<<endl;}
	flag=0;
}
	return 0;
}