# ACM NEERC 2019

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

#### Input

The first line contains two integers **w** and **h** (**4** ≤ **w**, **h** ≤ **5** * `10`

), 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 (^{8}**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** (**4** ≤ **n** ≤ **1000**), denoting the number of vertices in the polygon. Each of the following **n** lines contains two integers `x`

and _{i}`y`

(_{i}**1** ≤ `x`

≤ _{i}**w** − **1**, **1** ≤ `y`

≤ _{i}**h** − **1**), 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 (`x`

= _{i}`x`

, _{i+1}`x`

= _{n}`x`

) or horizontal (_{1}`y`

= _{i}`y`

, _{i+1}`y`

= _{n}`y`

), and none of them intersects another._{1}

The next line contains a single integer **t** (**1** ≤ **t** ≤ **100**), denoting the number of starting points for a shot. Each of the following **t** lines contains two integers `_x`

and _{i}_`_y`

(_{i}_**1** ≤ `_x`

≤ _{i}_**w** − **1**, **1** ≤ `_y`

≤ _{i}_**h** − **1**), 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.

#### Output

For each starting point in order of input, output either a single integer **−1** if the ball will never sink, or three integers `τ x`

, denoting the time before the ball sinks in seconds, and coordinates of the point where it happens (_{s} y_{s}**τ** > **0**, **1** ≤ `x`

≤ _{s}**w** − **1**, **1** ≤ `y`

≤ _{s}**h** − **1**). Point (`x`

, _{s}`y`

) must belong to the border of the pond._{s}

10 8 6 1 4 1 5 3 5 3 6 4 6 4 4 4 2 2 1 7 4 7 7 5

2 4 4 3 4 6 13 3 4 15 2 4

6 6 4 2 2 4 2 4 4 2 4 3 5 5 5 2 5 5

3 4 4 -1 3 4 4