import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum K = 30;

long solve(long A, long B, long C, long D) {
  long ans = abs(A - C) + abs(B - D);
  foreach_reverse (k; 0 .. K) {
    // blocked by r * r
    const r = 3L^^k;
    const a = A / r;
    const b = B / r;
    const c = C / r;
    const d = D / r;
    if (a == c && a % 3 == 1) {
      long y = min(b, d) + 1;
      for (; y % 3 != 1; ++y) {}
      if (y < max(b, d)) {
        const x0 = a * r - 1;
        const x1 = a * r + r;
        ans = abs(B - D) + min(abs(A - x0) + abs(C - x0), abs(A - x1) + abs(C - x1));
        break;
      }
    }
    if (b == d && b % 3 == 1) {
      long x = min(a, c) + 1;
      for (; x % 3 != 1; ++x) {}
      if (x < max(a, c)) {
        const y0 = b * r - 1;
        const y1 = b * r + r;
        ans = abs(A - C) + min(abs(B - y0) + abs(D - y0), abs(B - y1) + abs(D - y1));
        break;
      }
    }
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const A = readLong() - 1;
        const B = readLong() - 1;
        const C = readLong() - 1;
        const D = readLong() - 1;
        const ans = solve(A, B, C, D);
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
