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


bool isWhite(int k, long x, long y) {
  if (k == 0) {
    return true;
  } else {
    const r = 3L^^(k - 1);
    if (x / r == 1 && y / r == 1) {
      return false;
    }
    return isWhite(k - 1, x % r, y % r);
  }
}

int solveBrute(int k, int a, int b, int c, int d) {
  assert(isWhite(k, a, b));
  assert(isWhite(k, c, d));
  const n = 3^^k;
  auto dist = new int[][](n, n);
  foreach (x; 0 .. n) {
    dist[x][] = -1;
  }
  DList!int que;
  dist[a][b] = 0;
  que ~= a;
  que ~= b;
  for (; !que.empty; ) {
    const x = que.front; que.removeFront;
    const y = que.front; que.removeFront;
    if (x == c && y == d) {
      return dist[c][d];
    }
    foreach (dir; 0 .. 4) {
      const xx = x + [+1, 0, -1, 0][dir];
      const yy = y + [0, +1, 0, -1][dir];
      if (0 <= xx && xx < n && 0 <= yy && yy < n && isWhite(k, xx, yy)) {
        if (dist[xx][yy] == -1) {
          dist[xx][yy] = dist[x][y] + 1;
          que ~= xx;
          que ~= yy;
        }
      }
    }
  }
  assert(false);
}

long[] THREE;

long prevPassage(long a) {
  for (int k = 0; ; ++k) {
    if (a < THREE[k]) {
      return THREE[k] - 1 - nextPassage(THREE[k] - 1 - a);
    }
  }
}
long nextPassage(long a) {
  for (int k = 0; ; ++k) {
    if (a < THREE[k]) {
      foreach_reverse (i; 0 .. k) {
        if (a / THREE[i] % 3 == 1) {
          return (a / THREE[i] + 1) * THREE[i];
        }
      }
      return a;
    }
  }
}

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 / 3 == t / 3) {
      if (abs(b - d) < r) {
        ret = abs(a - c) + abs(b - d);
      } else {
        const x0 = prevPassage(a), x1 = nextPassage(a);
        ret = abs(b - d) + min(abs(a - x0) + abs(c - x0), abs(a - x1) + abs(c - x1));
      }
    } else if (s % 3 == t % 3) {
      if (abs(a - c) < r) {
        ret = abs(a - c) + abs(b - d);
      } else {
        const y0 = prevPassage(b), y1 = nextPassage(b);
        ret = abs(a - c) + min(abs(b - y0) + abs(d - y0), abs(b - y1) + abs(d - y1));
      }
    } else {
      ret = abs(a - c) + abs(b - d);
    }
  }
  debug {
    writefln("%s (%s, %s) (%s, %s): %s", k, a, b, c, d, ret);
  }
  return ret;
}

void main() {
  THREE = new long[30 + 1];
  foreach (i; 0 .. 30 + 1) {
    THREE[i] = 3L^^i;
  }
  
  debug {
    foreach (a; 0 .. 27) {
      writeln(a, ": ", prevPassage(a), " ", nextPassage(a));
    }
    foreach (a; 1 .. 27) {
      writeln(3L^^30 - a, ": ", prevPassage(3L^^30 - a), " ", nextPassage(3L^^30 - a));
    }
  }
  
  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);
        debug {
          const brt = solveBrute(4, cast(int)(A), cast(int)(B), cast(int)(C), cast(int)(D));
          assert(brt == ans, format("brt = %s", brt));
        }
      }
    }
  } catch (EOFException e) {
  }
}
