Problem: You have an ideally small racing car on an x-y plane (0 ≤ x, y ≤ 255, where bigger y denotes upper coordinate). The racing circuit course is figured by two solid walls. Each wall is a closed loop of connected line segments. End point coordinates of every line segment are both integers (See Figure 1). Thus, a wall is represented by a list of end point integer coordinates (x1, y1), (x2, y2), ...,(xn, yn). The start line and the goal line are identical.

<image>
Figure 1. A Simple Course

For the qualification run, you start the car at any integer coordinate position on the start line, say (sx, sy).

At any clock t (≥ 0), according to the acceleration parameter at t, (ax,t, ay,t), the velocity changes instantly to (vx,t-1 + ax,t, vy,t-1 + ay,t), if the velocity at t - 1 is (vx,t-1, vy,t-1). The velocity will be kept constant until the next clock. It is assumed that the velocity at clock -1, (vx,-1, vy,-1) is (0, 0). Each of the acceleration components must be either -1, 0, or 1, because your car does not have so fantastic engine and brake. In other words, any successive pair of velocities should not differ more than 1 for either x-component or y-component. Note that your trajectory will be piecewise linear as the walls are.

Your car should not touch nor run over the circuit wall, or your car will be crashed, even at the start line. The referee watches your car's trajectory very carefully, checking whether or not the trajectory touches the wall or attempts to cross the wall.

The objective of the qualification run is to finish your run within as few clock units as possible, without suffering from any interference by other racing cars. That is, you run alone the circuit around clockwise and reach, namely touch or go across the goal line without having been crashed. Don't be crashed even in the last trajectory segment after you reach the goal line. But we don't care whatever happens after that clock

Your final lap time is the clock t when you reach the goal line for the first time after you have once left the start line. But it needs a little adjustment at the goal instant. When you cross the goal line, only the necessary fraction of your car's last trajectory segment is counted. For example, if the length of your final trajectory segment is 3 and only its 1/3 fraction is needed to reach the goal line, you have to add only 0.333 instead of 1 clock unit.

Drivers are requested to control their cars as cleverly as possible to run fast but avoiding crash. ALAS! The racing committee decided that it is too DANGEROUS to allow novices to run the circuit. In the last year, it was reported that some novices wrenched their fingers by typing too enthusiastically their programs. So, this year, you are invited as a referee assistant in order to accumulate the experience on this dangerous car race.

A number of experienced drivers are now running the circuit for the qualification for semi-finals. They submit their driving records to the referee. The referee has to check the records one by one whether it is not a fake.

Now, you are requested to make a program to check the validity of driving records for any given course configuration. Is the start point right on the start line without touching the walls? Is every value in the acceleration parameter list either one of -1, 0, and 1? Does the length of acceleration parameter list match the reported lap time? That is, aren't there any excess acceleration parameters after the goal, or are these enough to reach the goal? Doesn't it involve any crash? Does it mean a clockwise running all around? (Note that it is not inhibited to run backward temporarily unless crossing the start line again.) Is the lap time calculation correct? You should allow a rounding error up to 0.01 clock unit in the lap time.



Input

The input consists of a course configuration followed by a number of driving records on that course.

A course configuration is given by two lists representing the inner wall and the outer wall, respectively. Each line shows the end point coordinates of the line segments that comprise the wall. A course configuration looks as follows:


ix,1 iy,1 ..... ix,N iy,N 99999
ox,1 oy,1 ..... ox,M oy,M 99999


where data are alternating x-coordinates and y-coordinates that are all non-negative integers (≤ 255) terminated by 99999. The start/goal line is a line segment connecting the coordinates (ix,1, iy,1) and (ox,1, oy,1). For simplicity, iy,1 is assumed to be equal to oy,1; that is, the start/goal line is horizontal on the x-y plane. Note that N and M may vary among course configurations, but do not exceed 100, because too many curves involve much danger even for experienced drivers. You need not check the validity of the course configuration.

A driving record consists of three parts, which is terminated by 99999: two integers sx, sy for the start position (sx, sy), the lap time with three digits after the decimal point, and the sequential list of acceleration parameters at all clocks until the goal. It is assumed that the length of the acceleration parameter list does not exceed 500. A driving record looks like the following:


sx sy
lap-time
ax,0 ay,0 ax,1 ay,1 ... ax,L ay,L 99999


Input is terminated by a null driving record; that is, it is terminated by a 99999 that immediately follows 99999 of the last driving record.

Output

The test result should be reported by simply printing OK or NG for each driving record, each result in each line. No other letter is allowed in the result.

Example

Input

6 28 6 32 25 32 26 27 26 24 6 24 99999
2 28 2 35 30 35 30 20 2 20 99999

3 28
22.667
0 1 1 1 1 0 0 -1 0 -1 1 0 0 0 1 0 -1 0 0 -1 -1 -1 -1 0 -1 0 -1 -1 -1 1 -1 1
-1 1 -1 0 1 0 1 1 1 1 1 0 1 1 99999

5 28
22.667
0 1 -1 1 1 0 1 -1 1 -1 1 0 1 0 1 0 -1 -1 -1 0 -1 -1 -1 0 -1 1 -1 -1 -1 1
-1 0 -1 1 -1 0 1 0 1 0 1 1 1 1 1 1 99999

4 28
6.333
0 1 0 1 1 -1 -1 -1 0 -1 0 -1 0 -1 99999

3 28
20.000
0 -1 1 -1 1 0 1 1 1 1 1 0 -1 0 -1 0 -1 1 -1 1
-1 1 -1 0 -1 -1 -1 -1 -1 -1 -1 0 1 0 1 -1 1 -1 1 -1 99999

99999


Output

OK
NG
NG
NG

Solution (in CPP):

Solution:
#include <iostream>
#include <iomanip>
#include <complex>
#include <vector>
#include <algorithm>
#include <cmath>
#include <array>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);
#define EQ(n,m) (abs((n)-(m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2>{
    L(const P& a, const P& b){ at(0)=a; at(1)=b; }
    L(){}
};

namespace std{
    bool operator < (const P& a, const P& b){
        return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;
    }
    bool operator == (const P& a, const P& b){
        return abs(a-b) < EPS;
    }
}

double dot(P a, P b){
    return (conj(a)*b).X;
}
double cross(P a, P b){
    return (conj(a)*b).Y;
}
int ccw(P a, P b, P c){
    b -= a;
    c -= a;
    if(cross(b,c) > EPS) return +1; //ccw
    if(cross(b,c) < -EPS) return -1; //cw
    if(dot(b,c) < -EPS) return +2; //c-a-b
    if(abs(c)-abs(b) > EPS) return -2; //a-b-c
    return 0; //a-c-b
}

bool intersectSS(const L& a, const L& b){
    return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&
        ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );
}
bool intersectSP(const L& s, const P &p){
    return abs(cross(s[0]-p, s[1]-p))<EPS  && dot(s[0]-p, s[1]-p)<EPS;
}

const int sentinel = 99999;
VP inputVP(int n=100){
    VP ret;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x;
        if(x == sentinel) break;
        cin >> y;
        ret.emplace_back(x, y);
    }
    return ret;
}

int main(){
    VP inwall = inputVP();
    VP outwall = inputVP();
    L startline(inwall[0], outwall[0]);
    while(1){
        VP tmp = inputVP(1);
        if(tmp.empty()) break;
        P start = tmp[0];
        double time;
        cin >> time;
        VP accel = inputVP();

        bool ng = false;
        //スタート位置
        if(!intersectSP(startline, start) || start == inwall[0] || start == outwall[0]){
            ng = true;
            //cerr << "start position error" << endl;
        }
        //加速度パラメータの数
        if((int)ceil(time) != (int)accel.size()){
            ng = true;
            //cerr << "acceleration parameter size error" << endl;
        }
        //加速度パラメータの値
        for(P a: accel){
            if(a.X < -1 || 1 < a.X || a.Y < -1 || 1 < a.Y){
                ng = true;
                //cerr << "acceleration parameter value error" << endl;
            }
        }
        if((inwall[0].X > outwall[0].X && accel[0].Y != 1)
            || (inwall[0].X < outwall[0].X && accel[0].Y != -1)){
            ng = true;
            //cerr << "back start" << endl;
        }

        //シミュレーション
        P pos = start;
        P v(0, 0);
        for(int i=0; i<(int)accel.size() && !ng; i++){
            v += accel[i];
            P next = pos +v;
            L line(pos, next);
            //壁に触れるか
            for(int i=0; i<(int)inwall.size(); i++){
                L wall(inwall[i], inwall[(i+1)%inwall.size()]);
                if(intersectSS(line, wall)){
                    ng = true;
                    //cerr << "touch inwall" << endl;
                }
            }
            for(int i=0; i<(int)outwall.size(); i++){
                L wall(outwall[i], outwall[(i+1)%outwall.size()]);
                if(intersectSS(line, wall)){
                    ng = true;
                    //cerr << "touch outwall" << endl;
                }
            }
            //スタートラインに触れるか
            if(i != 0 && intersectSS(line, startline)){
                //上からはNG
                if((inwall[0].X > outwall[0].X && pos.Y > inwall[0].Y)
                    || (inwall[0].X < outwall[0].X && pos.Y < inwall[0].Y)){
                    ng = true;
                    //cerr << "touch startline from above" << endl;
                }
                //下からならタイム計測
                double atime = i +(inwall[0].Y -pos.Y) /(next.Y -pos.Y);
                if(abs(time -atime) > 1e-2){
                    ng = true;
                    //cerr << "goal time error" << endl;
                    cout << atime << endl;
                }
            }
            pos = next;
        }
        if(ng){
            cout << "NG" << endl;
        }else{
            cout << "OK" << endl;
        }      
    }
    return 0;
}