Kile, a board games enthusiast, recently discovered the game Robots. The game consists of a board with rows and columns and one robot. The field is the top-left field of the board, while the field is the bottom-right.
At the beginning, the robot is positioned on some field (-th row, -th column), and the player can direct it in one of the four directions: up, down, left, or right. Depending on the chosen direction, it will move in that direction until it encounters its goal or a special field on the board. If at any point it wants to exit the board, it wraps around to the other side. For example, if it is located at the field and wants to move down, it will arrive at the field .
The board has three types of fields:
- Empty field — the robot continues moving in the same direction.
- Left turn field — when the robot steps on this field, it will turn left by and continue moving.
- Right turn field — when the robot steps on this field, it will turn right by and continue moving.
Most fields on the board are empty, only of them are left or right turn fields.
The game consists of rounds. In the -th round of the game, the robot will be placed on the field . The goal is to reach the field using the minimum number of turns, or determine that it is impossible.
After playing this game several times, Kile realized that it is more challenging than it initially seemed. That is why he needs your help now. Help him determine the minimum number of turns required for each round of the game!
Note: If the robot starts or finishes its path on a left or right turn field, that turn is not counted.
Input Specification
The first line contains integers , and , the number of rows, columns and non-empty fields on the board.
The -th of the following lines contains integers , and character L
or R
, the row and column of -th turn field and the type of turn. If L
then it is a left turn field. If R
then it is a right turn field.
The next line contains the integer , the number of rounds.
The -th of the following lines contains integers , , , , the starting position and the goal.
Output Specification
In the -th of the following lines output the minimal number of turns for the -th round of the game or -1
if it is impossible to reach the goal.
Scoring
Subtask | Points | Constraints |
---|---|---|
1 | 10 | |
2 | 13 | |
3 | 49 | |
4 | 38 | No additional constraints. |
Sample Input 1
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
Sample Output 1
-1
1
0
0
0
Sample Input 2
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
Sample Output 2
1
2
1
1
1
0
3
Explanation for Sample 2
First round: We start at . If we direct the robot to the left, it will arrive at in the next step because it wanted to exit the board, so it wraps around to the other side. Field is a left turn field, so the robot is now directed downwards. After two more steps, it will be at the desired goal .
Second round: We start at . If we direct the robot upwards, it will arrive at in two steps, where it will be directed to the left due to the left turn field. After two steps, it will be at the field , which is also a left turn field, so it will be directed downwards. In the next step, it will be at the desired goal .
Sample Input 3
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
Sample Output 3
2
1
1
0
-1
5
0
Comments