#include<iostream>
#include<queue>
#include<cstring>

typedef std::pair<int, int> P;

int w, h;
int field[100][100];
int xs, ys, xg, yg;
int n;
const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };

int main()
{
	while( std::cin >> w >> h, w | h )
	{
		memset( field, 0, sizeof( field ) );

		std::cin >> xs >> ys >> xg >> yg >> n;
		for( int i = 0; i != n; ++i )
		{
			int c, d, x, y;
			std::cin >> c >> d >> x >> y;
			for( int j = 0; j != 8; ++j )
			{
				int nx = x + ( d ? j % 2 : j / 2 ),
					ny = y + ( d ? j / 2 : j % 2 );

				field[ny-1][nx-1] = c;
			}
		}

		std::queue<P> que;
		int col = field[ys-1][xs-1];
		bool ans = false;
		que.push( P( xs, ys ) );

		while( !que.empty() )
		{
			P p = que.front(); que.pop();
			if( p.first == xg && p.second == yg )
			{
				ans = true;
				break;
			}

			field[p.second-1][p.first-1] = 0;

			for( int i = 0; i != 4; ++i )
			{
				int nx = p.first + dx[i],
					ny = p.second + dy[i];
				
				if( nx > 0 && nx <= w && ny > 0 && ny <= h && field[ny-1][nx-1] && field[ny-1][nx-1] == col )
					que.push( P( nx, ny ) );
			}
		}

		std::cout << ( ans ? "OK" : "NG" ) << std::endl;
	}

	return 0;
}
#include<stdio.h>
int main(void)
{
	int n,pm[10001],pe[10001],pj[10001],i,x[10001],y[10001];
	while(1){
		scanf("%d",&n);
		if(n==0) break;
		for(i=0;i<n;i++){
			scanf("%d %d %d",&pm[i],&pe[i],&pj[i]);
			x[i]=(pm[i]+pe[i])/2;
			y[i]=(pm[i]+pe[i]+pj[i])/3;
		}
		for(i=0;i<n;i++){
			if(pm[i]==100||pe[i]==100||pj[i]==100) printf("A\n");
			else if(x[i]>=90||y[i]>=80) printf("A\n");
			else if(y[i]>=70||y[i]>=50&&pm[i]>=80||pe[i]>=80) printf("B\n");
			else printf("C\n");
		}
	}
	return 0;
}	