#include<iostream>
using namespace std;
#define MAX 100
static const int dx[4] = {1, 0, -1, 0};
static const int dy[4] = {0, -1, 0, 1};
int w, h, sx, sy, gx, gy, G[MAX+2][MAX+2];
bool dfs( int x, int y, int color ){
 if ( color == 0 || G[x][y] != color ) return false;
 if ( x == gx && y == gy ) return true;
 G[x][y] = 0;
 for ( int r = 0; r < 4; r++ ){
if ( dfs(x + dx[r], y + dy[r], color) ) return true;
 }
 return false;
}
main(){
 int n, c, d, px, py, ww, hh;
 while( cin >> w >> h && w ){
cin >> sx >> sy >> gx >> gy >> n;
for ( int y = 0; y < h+2; y++ )
 for ( int x = 0; x < w+2; x++ ) G[x][y] = 0;
for ( int i = 1; i <=n; i++ ){
 cin >> c >> d >> px >> py;
 if ( d == 0 ) { ww = 4; hh = 2; }
 else { ww = 2; hh = 4; }
 for ( int y = 0; y <= hh-1; y++ )
for ( int x = 0; x <= ww-1; x++ ) G[px+x][py+y] = c;
}
if ( dfs(sx, sy, G[sx][sy]) ) cout << "OK" << endl;
else cout << "NG" << endl;
 }
}
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;

constexpr ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, const T b) {
    if(a >= b) return false;
    a = b;
    return true;
}
template <class T> inline bool chmin(T &a, const T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

/*-------------------------------------------*/

ll n, l, q;
ll x[100009], nxt[100009][17];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    cin >> n;
    rep(i, n) cin >> x[i];
    cin >> l;
    ll y = 0;
    rep(i, n) {
        while(y + 1 < n && x[i] + l >= x[y + 1]) {
            y++;
        }
        nxt[i][0] = y;
    }
    rep(i, 16) rep(j, n) nxt[j][i + 1] = nxt[nxt[j][i]][i];

    cin >> q;
    while(q--) {
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        if(b < a) swap(a, b);
        ll ans = 1;
        for(int i = 16; i >= 0; i--) {
            if(nxt[a][i] < b) {
                a = nxt[a][i];
                ans += 1 << i;
            }
        }
        cout << ans << endl;
    }

    return 0;
}