Beam of Light

Time limit: 0.3sMemory limit: 256MBInput: Output:

You are given a grid with NN rows and MM columns. The cell (x,y)(x,y), with 1≤x≤N1 \le x \le N and 1≤y≤M1 \le y \le M, is the square (x,x+1)×(y,y+1)(x,x+1) \times (y,y+1) of the plane, so the whole grid is the rectangle [1,N+1]×[1,M+1][1,N+1] \times [1,M+1]. The first coordinate xx is the row and the second coordinate yy is the column.

A beam of light starts at the point (x0,y0)(x_0,y_0), which is a corner of the cell (x0,y0)(x_0,y_0), and moves continuously with constant velocity (xv,yv)(x_v,y_v): in one unit of time it advances xvx_v units along the first axis and yvy_v units along the second axis. Whenever the beam hits a side of the grid it reflects, as a ray of light on a mirror, and when it reaches a corner of the grid it reflects on both sides at once, so it goes back the way it came. The beam never stops.

A traveling beam of light.\text{A traveling beam of light}.

You are given QQ queries of the form x yx\ y. For each query, determine if the beam will ever pass through the cell (x,y)(x,y). The beam passes through a cell if some point of its trajectory lies strictly inside the square of the cell. Touching only a corner of the cell does not count.

Input data

The first line of standard input contains two space-separated integers NN and MM, representing the number of rows and columns of the grid.

The second line of standard input contains four space-separated integers x0x_0, y0y_0, xvx_v, yvy_v, representing the initial coordinates of the beam and its velocity vector.

The third line contains QQ, the number of queries.

The next QQ lines contain two space-separated integers xx and yy, representing the cell of the grid asked by the query.

Output data

Each line ii of the next QQ lines of standard output must contain the answer to the ii-th query: YES if the beam passes through the cell, NO otherwise.

The output is not case-sensitive, so answers like yes or No will also be accepted.

Constraints and clarifications

  • 1≤N,M,xv,yv≤1 000 000 0001 \le N,M,x_v,y_v \le 1\ 000\ 000\ 000.
  • 1≤x0,x≤N1 \le x_0,x \le N.
  • 1≤y0,y≤M1 \le y_0,y \le M.
  • 1≤Q≤200 0001 \le Q \le 200\ 000.
# Score Constraints
0 0 Examples.
1 5 N=1N=1 or M=1M=1.
2 10 N,M,xv,yv≤100N,M,x_v,y_v \le 100.
3 20 xv=yvx_v=y_v and N+M≤1 000 000N+M \le 1\ 000\ 000.
4 65 No additional restrictions.

Example

stdin

6 9
2 3 1 1
6
2 3
1 3
3 6
4 8
6 9
5 1

stdout

YES
NO
NO
YES
NO
YES

Explanation

In the first sample case the grid has 66 rows and 99 columns, and the beam starts at the corner (2,3)(2,3) with velocity (1,1)(1,1), so it moves diagonally and reflects on the walls many times. The beam passes through the cells of the queries 11, 44 and 66, which are (2,3)(2,3), (4,8)(4,8) and (5,1)(5,1). It never passes through the cells of the queries 22, 33 and 55, which are (1,3)(1,3), (3,6)(3,6) and (6,9)(6,9).

First sample case. The numbers mark the queries in the order of the input.\text{First sample case. The numbers mark the queries in the order of the input.}

Example

stdin

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

stdout

YES
NO
YES
YES
NO
NO

Explanation

In the second sample case the grid has 66 rows and 88 columns, and the beam starts at the corner (2,3)(2,3) with velocity (1,2)(1,2), so it advances two columns for every row. The beam passes through the cells of the queries 11, 33 and 44, which are (2,3)(2,3), (3,6)(3,6) and (4,1)(4,1). It never passes through the cells of the queries 22, 55 and 66, which are (1,3)(1,3), (5,5)(5,5) and (6,7)(6,7).

Second sample case. The numbers mark the queries in the order of the input.\text{Second sample case. The numbers mark the queries in the order of the input.}

Log In or Sign Up to be able to send submissions!