favorite We need a little bit of your help to keep things running, click on this banner to learn more


Golf Time

Galina is an aspiring golfer and spends all her free time at a minigolf park. Her favorite golf course in this park is a rectangle, with one of its sides parallel to the north-south direction, and the other one parallel to the east-west direction. The ball is really small and can be considered a point.

Galina wants to practice a specific shot called "put". In this shot, a ball is placed somewhere inside the golf course and hit north-east, obtaining the speed of sqrt(2) inches per second. This means that the ball starts moving by exactly 1 inch to the north and 1 inch to the east per second.

The golf park uses an innovative technology that eliminates all friction, and the ball will continue to move at the initial speed regardless of the travelled distance. Collisions with golf course borders will be perfectly elastic. Fancy!

Inside the course there is a pond that is represented as a simple polygon with edges parallel to the sides of the golf course. If the ball touches the pond, it sinks momentarily.

You are given a list of starting coordinates for Galina’s next put training session. For each starting position, assuming a put is shot from this place, calculate how long the ball will travel before falling into the pond and find exactly where it happens, or determine that the ball will move indefinitely.


The first line contains two integers w and h (4w, h5 * 108), the width and the height of the golf course in inches. A coordinate system is introduced in such a way that the corners of the golf course have coordinates (0, 0), (w, 0), (w, h), (0, h), and the point (1, 1) is to the north-east of the point (0, 0).

The second line contains a single integer n (4n1000), denoting the number of vertices in the polygon. Each of the following n lines contains two integers xi and yi (1xiw1, 1yih1), denoting the coordinates of the i-th vertex. The vertices are listed in traversal order.

All polygon vertices are distinct and none of them lie at the polygon's edge. All polygon edges are either vertical (xi = xi+1, xn = x1) or horizontal (yi = yi+1, yn = y1), and none of them intersects another.

The next line contains a single integer t (1t100), denoting the number of starting points for a shot. Each of the following t lines contains two integers _xi_ and _yi_ (1_xi_w1, 1_yi_h1), denoting the coordinates of the i-th starting point. No starting point lies inside or on the border of the pond.

Note that all starting points are independent of each other. In particular, you can assume that there is exactly one ball inside the golf course at any moment.


For each starting point in order of input, output either a single integer −1 if the ball will never sink, or three integers τ xs ys, denoting the time before the ball sinks in seconds, and coordinates of the point where it happens (τ > 0, 1xsw1, 1ysh1). Point (xs, ys) must belong to the border of the pond.

Time limit 1 second
Memory limit 128 MiB
Input example #1
10 8
1 4
1 5
3 5
3 6
4 6
4 4
2 2
1 7
4 7
7 5
Output example #1
2 4 4
3 4 6
13 3 4
15 2 4
Input example #2
6 6
2 2
4 2
4 4
2 4
5 5
5 2
5 5
Output example #2
3 4 4
3 4 4
Source 2019 ACM NEERC, Северный регион, Октябрь 26, Задача G