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)); }


long[] THREE;

long solve(int k, long a, long b, long c, long d) {
  long ret;
  if (k == 0) {
    ret = 0;
  } else {
    const r = THREE[k - 1];
    const s = (a / r) * 3 + (b / r);
    const t = (c / r) * 3 + (d / r);
    /*
      012
      345
      678
    */
    if (s == t) {
      ret = solve(k - 1, a % r, b % r, c % r, d % r);
    } else if (s == 1 && t == 7 || s == 7 && t == 1) {
      const y0 = r - 1, y1 = 2 * r;
      ret = abs(a - c) + min(abs(b - y0) + abs(d - y0), abs(b - y1) + abs(d - y1));
    } else if (s == 3 && t == 5 || s == 5 && t == 3) {
      const x0 = r - 1, x1 = 2 * r;
      ret = abs(b - d) + min(abs(a - x0) + abs(c - x0), abs(a - x1) + abs(c - x1));
    } else {
      ret = abs(a - c) + abs(b - d);
    }
  }
  return ret;
}

void main() {
  THREE = new long[30 + 1];
  foreach (i; 0 .. 30 + 1) {
    THREE[i] = 3L^^i;
  }
  
  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(30, A, B, C, D);
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
