You are given a fractal of level K, defined as a grid with one white square for level 0, and for K > 0, a 3^K x 3^K grid with the central subgrid being black and the other eight subgrids being fractals of level K-1. Given Q quadruples of integers (a_i, b_i, c_i, d_i), find the minimum integer n such that there exists a sequence of white squares from (a_i, b_i) to (c_i, d_i) where each pair of adjacent squares shares a side. Constraints: 1 ≤ Q ≤ 10000, 1 ≤ a_i, b_i, c_i, d_i ≤ 3^30, (a_i, b_i) ≠ (c_i, d_i), and (a_i, b_i) and (c_i, d_i) are white squares. The input format is Q followed by Q lines of quadruples, and the output should contain Q lines with the distances from (a_i, b_i) to (c_i, d_i).

# Buggy version:
s=lambda t,i:0--t//3**i
for _ in range(int(input())):
  a,b,c,d=map(int,input().split());mx=max([i for i in range(30) if s(a,i)==s(c,i)and s(a,i)%3==2and abs(s(b,i)-s(d,i))>=2]+[-1])+1;my=max([i for i in range(30) if s(b,i)==s(d,i)and s(b,i)%3==2and abs(s(a,i)-s(c,i))>=2]+[-1])+1
  if mx<my:mx,a,b,c,d=my,b,a,d,c
  mi=3**(mx-1);ma=2*mi+1;ac=(3*mi)*((0--a//(3*mi))-1);a-=ac;c-=ac;print(abs(b-d)+min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c)))

# Fixed version:
s=lambda t,i:0--t//3**i
for _ in range(int(input())):
  a,b,c,d=map(int,input().split());mx=max([i for i in range(30) if s(a,i)==s(c,i)and s(a,i)%3==2and abs(s(b,i)-s(d,i))>=2]+[-1])+1;my=max([i for i in range(30) if s(b,i)==s(d,i)and s(b,i)%3==2and abs(s(a,i)-s(c,i))>=2]+[-1])+1
  if mx==my==0:print(abs(b-d)+abs(a-c));continue
  if mx<my:mx,a,b,c,d=my,b,a,d,c
  mi=3**(mx-1);ma=2*mi+1;ac=(3*mi)*((0--a//(3*mi))-1);a-=ac;c-=ac;print(abs(b-d)+min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c)))

# Buggy version:
def solve():
    a, b, c, d = map(int, input().split())
    a -= 1
    b -= 1
    c -= 1
    d -= 1
    if a > c:
        a, b, c, d = c, d, a, b
    for k in range(30, -1, -1):
        if a // 3 ** k == c // 3 ** k and a // 3 ** k % 3 == 1 \
                and min(b, d) // 3 ** k % 3 == 0 and max(b, d) // 3 ** k % 3 == 2:
            print(min(a % 3 ** k + 1 + c % 3 ** k + 1 + abs(b - d),
                3 ** k - a % 3 ** k + 3 ** k - c % 3 ** k + abs(b - d)))
            return
        if b // 3 ** k == d // 3 ** k and b // 3 ** k % 3 == 1:
                and min(a, c) // 3 ** k % 3 == 0 and max(a, c) // 3 ** k % 3 == 2:
            print(min(b % 3 ** k + 1 + d % 3 ** k + 1 + abs(a - c),
                3 ** k - b % 3 ** k + 3 ** k - d % 3 ** k + abs(a - c)))
            return
    print(abs(a - c) + abs(b - d))

q = int(input())
for _ in range(q):
    solve()

# Fixed version:
def solve():
    a, b, c, d = map(int, input().split())
    a -= 1
    b -= 1
    c -= 1
    d -= 1
    if a > c:
        a, b, c, d = c, d, a, b
    for k in range(30, -1, -1):
        def check(a, b, c, d):
            if a // 3 ** k == c // 3 ** k and a // 3 ** k % 3 == 1 \
                    and (min(b, d) // 3 ** k % 3 == 0 and max(b, d) // 3 ** k % 3 == 2
                            or max(b, d) // 3 ** k - min(b, d) // 3 ** k >= 3):
                print(min(a % 3 ** k + 1 + c % 3 ** k + 1 + abs(b - d),
                    3 ** k - a % 3 ** k + 3 ** k - c % 3 ** k + abs(b - d)))
                return True
        if check(a, b, c, d) or check(b, a, d, c):
            return
    print(abs(a - c) + abs(b - d))

q = int(input())
for _ in range(q):
    solve()