### constant

ENDV = 99999
DELTA = 0.01
PI = Math::PI
PI2 = PI * 2

### global variables

$rbuf = []

### subroutines

def read_word
  while $rbuf.empty?
    line = gets
    return nil if line.nil?
    $rbuf = line.strip.split
  end
  $rbuf.shift
end

def read_int
  read_word.to_i
end

def read_float
  read_word.to_f
end

def read_points
  pts = []
  loop do
    x = read_int
    break if x == ENDV
    pts << [x, read_int]
  end
  pts
end

def calc_traj(sx, sy, acs)
  traj = [[sx, sy]]
  vx = vy = 0

  acs.each_with_index do |ac, i|
    vx += ac[0]
    vy += ac[1]
    x0, y0 = traj[i]
    traj[i + 1] = [x0 + vx, y0 + vy]
  end

  traj
end

def i_prod(v0x, v0y, v1x, v1y)
  v0x * v1x + v0y * v1y
end

def o_prod(v0x, v0y, v1x, v1y)
  v0x * v1y - v1x * v0y
end

def col_segs?(s0x0, s0y0, s0x1, s0y1, s1x0, s1y0, s1x1, s1y1, ts = nil)
  v0x = s0x1 - s0x0
  v0y = s0y1 - s0y0
  v1x = s1x1 - s1x0
  v1y = s1y1 - s1y0
  vx = s1x0 - s0x0
  vy = s1y0 - s0y0

  cr_01 = o_prod(v0x, v0y, v1x, v1y)
  cr_v0 = o_prod(vx, vy, v0x, v0y)
  cr_v1 = o_prod(vx, vy, v1x, v1y)

  if cr_01 == 0		# parallel
    if cr_v0 == 0	# on line
      if i_prod(s1x0 - s0x0, s1y0 - s0y0, s1x1 - s0x0, s1y1 - s0y0) < 0 ||
         i_prod(s1x0 - s0x1, s1y0 - s0y1, s1x1 - s0x1, s1y1 - s0y1) < 0 ||
         i_prod(s0x0 - s1x0, s0y0 - s1y0, s0x1 - s1x0, s0y1 - s1y0) < 0 ||
         i_prod(s0x0 - s1x1, s0y0 - s1y1, s0x1 - s1x1, s0y1 - s1y1) < 0
        return true
      end
    end
    return false
  end

  t0 = cr_v1.to_f / cr_01
  t1 = cr_v0.to_f / cr_01

  if t0 < 0.0 || t0 > 1.0 || t1 < 0.0 || t1 > 1.0	# not collide
    return false
  end

  if ts
    ts[0] = t0
    ts[1] = t1
  end
  
  true
end

def crash?(traj, ntraj, ws, nws)
  for wi in (0...nws)
    wj = (wi + 1) % nws

    traj.each_cons(2) do |tr0, tr1|
      if col_segs?(*tr0, *tr1, *ws[wi], *ws[wj])
        return true
      end
    end
  end
  false
end

def valid?(sx, sy, lt, acs)
  # Is the start point right on the start line without touching the walls?

  sl0 = $iws[0]
  sl1 = $ows[0]
  if sl0[1] != sl1[1] || sy != sl0[1] ||
     ! ((sl0[0] < sx && sx < sl1[0]) || (sl1[0] < sx && sx < sl0[0]))
    return false
  end    

  # Is every value in the acceleration parameter list either one of
  # -1, 0, and 1?

  acs.each do |dx, dy|
    return false if dx < -1 || dx > 1 || dy < -1 || dy > 1
  end

  # 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?

  traj = calc_traj(sx, sy, acs)
  ntraj = traj.length
  #p traj; exit

  for i in (1...(ntraj - 2))
    if col_segs?(*traj[i], *traj[i + 1], *sl0, *sl1)
      return false
    end
  end  

  ts = []
  if ! col_segs?(*traj[ntraj - 2], *traj[ntraj - 1], *sl0, *sl1, ts)
    return false
  end

  tt = (ntraj - 2) + ts[0]
  #p [lt, tt]
  
  # Doesn't it involve any crash?

  if crash?(traj, ntraj, $iws, $niws) || crash?(traj, ntraj, $ows, $nows)
    return false
  end  

  # Does it mean a clockwise running all around?
  # (Note that it is not inhibited to run backward temporarily unless
  #  crossing the start line again.)

  cx = (sl1[0] > sl0[0]) ? sl0[0] - 0.5 : sl0[0] + 0.5
  cy = sl0[1].to_f

  sum_th = 0.0
  prv_th = Math.atan2(traj[0][1] - cy, traj[0][0] - cx)
  for i in (1...ntraj)
    th = Math.atan2(traj[i][1] - cy, traj[i][0] - cx)
    dth = th - prv_th
    dth -= PI2 if dth > PI
    dth += PI2 if dth < -PI
    sum_th += dth
    prv_th = th
  end

  if sum_th > -PI2
    return false
  end
  
  # Is the lap time calculation correct?
  # You should allow a rounding error up to 0.01 clock unit in the lap time.

  if (lt - tt).abs > DELTA
    return false
  end
  
  true
end

### main

$iws = read_points
$ows = read_points
#p $iws
#p $ows

$niws = $iws.length
$nows = $ows.length

loop do
  sx = read_int
  break if sx == ENDV

  sy = read_int
  lt = read_float
  acs = read_points
  #p [sx, sy, lt]
  #p acs

  puts valid?(sx, sy, lt, acs) ? "OK" : "NG"
end

Measured memory_bytes: 7168