Under construction

Still being built. Things may change

Robots

Time limit2sMemory limit1024 MB

Summary
On a wrapping board with left and right turn fields, find the fewest turns for a robot to get from a start cell to a goal cell for each query, or -1 if impossible.
Level

Hard9 of 10

Topics
Graph, Shortest path, Implementation
Solved
No attempts yet

Problem

Kile, a board game enthusiast, recently discovered the game Robots. The game uses a board with nn rows and mm columns and one robot. The field (1,1)(1, 1) is the top-left field of the board, and the field (n,m)(n, m) is the bottom-right field.

At the start, the robot is placed on a field (x,y)(x, y) (xx-th row, yy-th column). The player can direct it in one of four directions: up, down, left, or right. The robot moves in the chosen direction until it reaches the goal or a special field. If it tries to leave the board, it wraps around to the opposite side. For example, if the robot is at (n,3)(n, 3) and moves down, it arrives at (1,3)(1, 3).

The board has three types of fields:

  • Empty field: the robot keeps moving in the same direction.
  • Left turn field: when the robot steps on it, the robot turns left by 90° and keeps moving.
  • Right turn field: when the robot steps on it, the robot turns right by 90° and keeps moving.

Most fields are empty. Only kk fields are left or right turn fields.

The game has qq rounds. In round ii, the robot is placed on (ai,bi)(a_i, b_i). The goal is to reach (ci,di)(c_i, d_i) with the minimum number of turns, or determine that it is impossible.

Note: If the robot starts or finishes on a left or right turn field, that turn is not counted.

Input

The first line contains integers nn, mm, and kk (1≤n,m≤1061 \le n, m \le 10^6, 0≤k≤1050 \le k \le 10^5), the number of rows, the number of columns, and the number of non-empty fields on the board.

Each of the following kk lines contains integers xix_i, yiy_i and a character sis_i (1≤xi≤n1 \le x_i \le n, 1≤yi≤m1 \le y_i \le m, sis_i is L or R). The pair (xi,yi)(x_i, y_i) is the row and column of the ii-th turn field, and sis_i is its type. L is a left turn field and R is a right turn field.

The next line contains an integer qq (1≤q≤3⋅1051 \le q \le 3 \cdot 10^5), the number of rounds.

Each of the following qq lines contains integers aia_i, bib_i, cic_i, did_i (1≤ai,ci≤n1 \le a_i, c_i \le n, 1≤bi,di≤m1 \le b_i, d_i \le m), the start and the goal of the round.

Output

For each round, output the minimum number of turns on its own line, or -1 if the goal cannot be reached.

Examples3

  1. Example 1

    Input
    2 2 2
    1 1 L
    2 2 R
    5
    1 1 2 2
    2 1 1 2
    1 1 1 2
    2 1 1 1
    2 2 2 1
    
    Expected output
    -1
    1
    0
    0
    0
    
  2. Example 2

    Input
    3 3 4
    1 1 L
    1 3 L
    2 1 L
    3 3 L
    7
    1 1 3 3
    3 3 2 1
    3 1 2 2
    2 3 1 2
    2 3 3 1
    1 2 3 2
    3 3 2 2
    
    Expected output
    1
    2
    1
    1
    1
    0
    3
    
  3. Example 3

    Input
    4 4 8
    1 1 R
    1 3 L
    2 2 R
    2 4 L
    3 1 L
    3 3 L
    4 2 L
    4 4 L
    7
    1 2 1 4
    2 2 3 4
    4 4 3 2
    4 1 4 4
    4 2 3 1
    1 2 2 3
    2 4 2 3
    
    Expected output
    2
    1
    1
    0
    -1
    5
    0