Under construction

Still being built. Things may change

Golf Time

Time limit2sMemory limit512 MB

Summary
For each start point, find when a diagonal ball bouncing off the course walls first hits the pond and where, or report that it never does.
Level

Hard8 of 10

Topics
Simulation, Math, Number theory
Solved
No attempts yet

Problem

Galina is an aspiring golfer who spends most of her free time at a minigolf park. Her favorite course is a rectangle, with one side parallel to the north-south direction and the other parallel to the east-west direction. The ball is very small and can be treated as a point.

Galina wants to practice a shot called "put". In this shot, a ball is placed somewhere inside the course and hit north-east at a speed of 2\sqrt{2} inches per second. That means the ball moves 1 inch north and 1 inch east every second.

The park uses an innovative technology that removes all friction. The ball keeps its initial speed no matter how far it travels. Collisions with the course border are perfectly elastic.

A pond sits inside the course. It is a simple polygon whose edges are parallel to the sides of the course. The ball sinks the moment it touches the pond.

You are given the starting positions for Galina's next put practice session. For each starting position, find how long the ball travels before it falls into the pond, and find exactly where it falls in. If the ball moves forever, report that.

Input

The first line contains two integers ww and hh, the width and height of the course in inches, where 4≤w,h≤5⋅1084 \le w, h \le 5 \cdot 10^8. The coordinate system places the corners at (0,0)(0, 0), (w,0)(w, 0), (w,h)(w, h), and (0,h)(0, h), and the point (1,1)(1, 1) is north-east of (0,0)(0, 0).

The second line contains nn, the number of vertices of the pond polygon (4≤n≤10004 \le n \le 1000).

Each of the next nn lines contains the coordinates xix_i and yiy_i of a vertex, listed in traversal order, where 1≤xi≤w−11 \le x_i \le w - 1 and 1≤yi≤h−11 \le y_i \le h - 1. All vertices are distinct, and none of them lies on the polygon's edge. Every edge is vertical (xi=xi+1x_i = x_{i+1}, with xn=x1x_n = x_1) or horizontal (yi=yi+1y_i = y_{i+1}, with yn=y1y_n = y_1), and no two edges intersect.

The next line contains tt (1≤t≤1001 \le t \le 100), the number of starting points. Each of the next tt lines contains the coordinates xix_i and yiy_i of a starting point, where 1≤xi≤w−11 \le x_i \le w - 1 and 1≤yi≤h−11 \le y_i \le h - 1. No starting point lies inside or on the border of the pond. The starting points are independent, so at any moment exactly one ball is on the course.

Output

For each starting point, in input order, print -1 if the ball never sinks. Otherwise print three integers τ\tau, xsx_s, and ysy_s: the time in seconds before the ball sinks (τ>0\tau > 0), and the point where it sinks (1≤xs≤w−11 \le x_s \le w - 1, 1≤ys≤h−11 \le y_s \le h - 1). The point must lie on the border of the pond.

Hints

In the first example, the ball trajectories look as follows.

In the second example, starting positions 1 and 3 coincide, and the ball from position 2 never stops.

Examples2

  1. Example 1

    Input
    10 8
    6
    1 4
    1 5
    3 5
    3 6
    4 6
    4 4
    4
    2 2
    1 7
    4 7
    7 5
    
    Expected output
    2 4 4
    3 4 6
    13 3 4
    15 2 4
    
  2. Example 2

    Input
    6 6
    4
    2 2
    4 2
    4 4
    2 4
    3
    5 5
    5 2
    5 5
    
    Expected output
    3 4 4
    -1
    3 4 4