#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int meiro[110][110];
int cc;
int w,h;
int xx[]={1,-1,0,0};
int yy[]={0,0,1,-1};

void dfs(int x,int y){
	meiro[y][x]=-1;
	int i;
	for(i=0;i<4;i++){
		if(x+xx[i]<0||w<=x+xx[i]||y+yy[i]<0||h<=y+yy[i])continue;
		if(meiro[y+yy[i]][x+xx[i]]!=cc)continue;
		dfs(x+xx[i],y+yy[i]);
	}
}

int main() {
	int xs,ys,xg,yg,n,c,d,x,y;
	while(w=in(),h=in()){
		cin>>xs>>ys>>xg>>yg>>n;
		xs--;ys--;xg--;yg--;
		int i;
		memset(meiro,0,sizeof(meiro));
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			x--;y--;
//cout<<c<<" "<<d<<" "<<x<<" "<<y<<endl;
			if(d==0){
				meiro[y][x]=meiro[y][x+1]=meiro[y][x+2]=meiro[y][x+3]=meiro[y+1][x]=meiro[y+1][x+1]=meiro[y+1][x+2]=meiro[y+1][x+3]=c;
			}else{
				meiro[y][x]=meiro[y][x+1]=meiro[y+1][x]=meiro[y+1][x+1]=meiro[y+2][x]=meiro[y+2][x+1]=meiro[y+3][x]=meiro[y+3][x+1]=c;
			}
		}
		if(meiro[ys][xs]==0){
			cout<<"NG\n";
			continue;
		}
		cc=meiro[ys][xs];
//cout<<"hoge\n";
		dfs(xs,ys);
//cout<<"piyo\n";
		if(meiro[yg][xg]==-1)cout<<"OK\n";
		else cout<<"NG\n";
	}
	return 0;
}
#include<bits/stdc++.h>
#define pf(x) x*x
using namespace std;
int n, d[2], col[2][360007];
vector<pair<int, int> >e[2];
int id(int a, int b) { return a * 2 * n + b; }
int check(int pos) { return pos >= 0 && pos < 2 * n; }
int dfs(int p, int a, int b)
{
	for (int i = 0, lim = e[p].size(); i < lim; i++)
	{
		int c = a + e[p][i].first, d = b + e[p][i].second;
		if (!check(c) || !check(d))continue;
		if (!col[p][id(c, d)])
		{
			col[p][id(c, d)] = -col[p][id(a, b)];
			dfs(p, c, d);
		}
	}
	return 0;
}
int main()
{
	cin >> n >> d[0] >> d[1];
	for (int p = 0; p <= 1; p++)
		for (int x = 0; x <= d[p]; x++)
		{
			int y = sqrt(d[p] - x * x);
			if (x * x + y * y != d[p])continue;
			e[p].push_back(make_pair(x, y));
			e[p].push_back(make_pair(-x, y));
			e[p].push_back(make_pair(x, -y));
			e[p].push_back(make_pair(-x, -y));
		}
	for (int i = 0; i < 2 * n; i++)
		for (int j = 0; j < 2 * n; j++)
			for (int p = 0; p <= 1; p++)
				if (!col[p][id(i, j)])
				{
					col[p][id(i, j)] = 1;
					dfs(p, i, j);
				}
	int now,haha;
	int tot = 0;
	for (int i = 0; i < 2 * n; i++)
		for (int j = 0; j < 2 * n; j++)
			if (col[0][id(i, j)] == 1 && col[1][id(i, j)] == 1)
			{
				printf("%d %d\n", i, j);
				if (++tot == n * n)return 0;
			}
	return 0;
}