#P1681E. Labyrinth Adventures

    ID: 7690 Type: RemoteJudge 6000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>data structuresdpmatricesshortest paths*2600

Labyrinth Adventures

No submission language available for this problem.

Description

You found a map of a weirdly shaped labyrinth. The map is a grid, consisting of nn rows and nn columns. The rows of the grid are numbered from 11 to nn from bottom to top. The columns of the grid are numbered from 11 to nn from left to right.

The labyrinth has nn layers. The first layer is the bottom left corner (cell (1,1)(1, 1)). The second layer consists of all cells that are in the grid and adjacent to the first layer by a side or a corner. The third layer consists of all cells that are in the grid and adjacent to the second layer by a side or a corner. And so on.

The labyrinth with 55 layers, for example, is shaped as follows:

The layers are separated from one another with walls. However, there are doors in these walls.

Each layer (except for layer nn) has exactly two doors to the next layer. One door is placed on the top wall of the layer and another door is placed on the right wall of the layer. For each layer from 11 to n1n-1 you are given positions of these two doors. The doors can be passed in both directions: either from layer ii to layer i+1i+1 or from layer i+1i+1 to layer ii.

If you are standing in some cell, you can move to an adjacent by a side cell if a wall doesn't block your move (e.g. you can't move to a cell in another layer if there is no door between the cells).

Now you have mm queries of sort: what's the minimum number of moves one has to make to go from cell (x1,y1)(x_1, y_1) to cell (x2,y2)(x_2, y_2).

The first line contains a single integer nn (2n1052 \le n \le 10^5) — the number of layers in the labyrinth.

The ii-th of the next n1n-1 lines contains four integers d1,x,d1,y,d2,xd_{1,x}, d_{1,y}, d_{2,x} and d2,yd_{2,y} (1d1,x,d1,y,d2,x,d2,yn1 \le d_{1,x}, d_{1,y}, d_{2,x}, d_{2,y} \le n) — the coordinates of the doors. Both cells are on the ii-th layer. The first cell is adjacent to the top wall of the ii-th layer by a side — that side is where the door is. The second cell is adjacent to the right wall of the ii-th layer by a side — that side is where the door is.

The next line contains a single integer mm (1m21051 \le m \le 2 \cdot 10^5) — the number of queries.

The jj-th of the next mm lines contains four integers x1,y1,x2x_1, y_1, x_2 and y2y_2 (1x1,y1,x2,y2n1 \le x_1, y_1, x_2, y_2 \le n) — the coordinates of the cells in the jj-th query.

For each query, print a single integer — the minimum number of moves one has to make to go from cell (x1,y1)(x_1, y_1) to cell (x2,y2)(x_2, y_2).

Input

The first line contains a single integer nn (2n1052 \le n \le 10^5) — the number of layers in the labyrinth.

The ii-th of the next n1n-1 lines contains four integers d1,x,d1,y,d2,xd_{1,x}, d_{1,y}, d_{2,x} and d2,yd_{2,y} (1d1,x,d1,y,d2,x,d2,yn1 \le d_{1,x}, d_{1,y}, d_{2,x}, d_{2,y} \le n) — the coordinates of the doors. Both cells are on the ii-th layer. The first cell is adjacent to the top wall of the ii-th layer by a side — that side is where the door is. The second cell is adjacent to the right wall of the ii-th layer by a side — that side is where the door is.

The next line contains a single integer mm (1m21051 \le m \le 2 \cdot 10^5) — the number of queries.

The jj-th of the next mm lines contains four integers x1,y1,x2x_1, y_1, x_2 and y2y_2 (1x1,y1,x2,y2n1 \le x_1, y_1, x_2, y_2 \le n) — the coordinates of the cells in the jj-th query.

Output

For each query, print a single integer — the minimum number of moves one has to make to go from cell (x1,y1)(x_1, y_1) to cell (x2,y2)(x_2, y_2).

Samples

Sample Input 1

2
1 1 1 1
10
1 1 1 1
1 1 1 2
1 1 2 1
1 1 2 2
1 2 1 2
1 2 2 1
1 2 2 2
2 1 2 1
2 1 2 2
2 2 2 2

Sample Output 1

0
1
1
2
0
2
1
0
1
0

Sample Input 2

4
1 1 1 1
2 1 2 2
3 2 1 3
5
2 4 4 3
4 4 3 3
1 2 3 3
2 2 4 4
1 4 2 3

Sample Output 2

3
4
3
6
2

Note

Here is the map of the labyrinth from the second example. The doors are marked red.