#PANASONIC2020F. Fractal Shortest Path

Fractal Shortest Path

Score : 600600 points

Problem Statement

For a non-negative integer KK, we define a fractal of level KK as follows:

  • A fractal of level 00 is a grid with just one white square.
  • When K>0K > 0, a fractal of level KK is a 3K×3K3^K \times 3^K grid. If we divide this grid into nine 3K1×3K13^{K-1} \times 3^{K-1} subgrids:- The central subgrid consists of only black squares.
    • Each of the other eight subgrids is a fractal of level K1K-1.
  • The central subgrid consists of only black squares.
  • Each of the other eight subgrids is a fractal of level K1K-1.

For example, a fractal of level 22 is as follows:

A fractal of level 2

In a fractal of level 3030, let (r,c)(r, c) denote the square at the rr-th row from the top and the cc-th column from the left.

You are given QQ quadruples of integers (ai,bi,ci,di)(a_i, b_i, c_i, d_i). For each quadruple, find the distance from (ai,bi)(a_i, b_i) to (ci,di)(c_i, d_i).

Here the distance from (a,b)(a, b) to (c,d)(c, d) is the minimum integer nn that satisfies the following condition:

  • There exists a sequence of white squares (x0,y0),,(xn,yn)(x_0, y_0), \ldots, (x_n, y_n) satisfying the following conditions:- (x0,y0)=(a,b)(x_0, y_0) = (a, b)
    • (xn,yn)=(c,d)(x_n, y_n) = (c, d)
    • For every i(0in1)i (0 \leq i \leq n-1), (xi,yi)(x_i, y_i) and (xi+1,yi+1)(x_{i+1}, y_{i+1}) share a side.
  • (x0,y0)=(a,b)(x_0, y_0) = (a, b)
  • (xn,yn)=(c,d)(x_n, y_n) = (c, d)
  • For every i(0in1)i (0 \leq i \leq n-1), (xi,yi)(x_i, y_i) and (xi+1,yi+1)(x_{i+1}, y_{i+1}) share a side.

Constraints

  • 1Q100001 \leq Q \leq 10000
  • 1ai,bi,ci,di3301 \leq a_i, b_i, c_i, d_i \leq 3^{30}
  • (ai,bi)(ci,di)(a_i, b_i) \neq (c_i, d_i)
  • (ai,bi)(a_i, b_i) and (ci,di)(c_i, d_i) are white squares.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

QQ

a1 b1 c1 d1a_1 \ b_1 \ c_1 \ d_1

::

aQ bQ cQ dQa_Q \ b_Q \ c_Q \ d_Q

Output

Print QQ lines. The ii-th line should contain the distance from (ai,bi)(a_i, b_i) to (ci,di)(c_i, d_i).

2
4 2 7 4
9 9 1 9
5
8