#pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <typename T>
using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
//*
constexpr ll MOD = 1e9 + 7;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T>
inline bool chmax(S &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename S, typename T>
inline bool chmin(S &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T>
bool print_(const T &a) {
  cout << a;
  return true;
}
template <typename T>
bool print_(const vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) {
      cout << " ";
    }
  }
  return false;
}
template <typename T>
bool print_(const vector<vector<T>> &vv) {
  for (auto &v : vv) {
    for (auto &a : v) {
      cout << a;
      if (&a != &v.back()) {
        cout << " ";
      }
    }
    if (&v != &vv.back()) {
      cout << "\n";
    }
  }
  return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&... tail) {
  bool f = print_(head);
  if (sizeof...(tail) != 0) {
    cout << (f ? " " : "\n");
  }
  print(forward<Tail>(tail)...);
}
#pragma endregion

struct Tri {
  ll n;
  vi digit;
  Tri() : n(0), digit(vi(30, 0)) {}
  Tri(ll n_): n(n_), digit(vi(30, 0)) {
    reset();
  }
  void reset() {
    ll n_ = n;
    rep(i, 30){
      digit[i] = n_ % 3;
      n_ /= 3;
    }
  }
  void reset_inv() {
    ll c = 1;
    n = 0;
    rep(i, 30){
      n += c * digit[i];
      c *= 3;
    }
  }
  Tri &operator+=(const Tri &p) {
    n += p.n;
    reset();
    return *this;
  }
  Tri &operator-=(const Tri &p) {
    n -= p.n;
    reset();
    return *this;
  }
  Tri &operator*=(const Tri &p) {
    n *= p.n;
    reset();
    return *this;
  }
  Tri &operator/=(const Tri &p) {
    n /= p.n;
    reset();
    return *this;
  }
  Tri operator-() { return Tri(-n); }
  Tri operator+(const Tri &p) { return Tri(*this) += p; }
  Tri operator-(const Tri &p) { return Tri(*this) -= p; }
  Tri operator*(const Tri &p) { return Tri(*this) *= p; }
  Tri operator/(const Tri &p) { return Tri(*this) /= p; }
  bool operator>(const Tri &p) { return n > p.n; }
  bool operator<(const Tri &p) { return n < p.n; }
  bool operator>=(const Tri &p) { return n >= p.n; }
  bool operator<=(const Tri &p) { return n <= p.n; }
  bool operator==(const Tri &p) { return n == p.n; }
  bool operator!=(const Tri &p) { return n != p.n; }
  int& operator[](size_t s) { return digit[s]; }
  friend ostream &operator<<(ostream &os, const Tri &p) {
    return os << p.n;
  }
};

Tri abs(Tri a) {
  return Tri(abs(a.n));
}

// a ** b O(b)
Tri pow(Tri a, ll b) {
  Tri ret(1);
  while(b--) ret *= a;
  return ret;
}

Tri min(Tri a, Tri b) {
  return a < b ? a : b;
}

Tri max(Tri a, Tri b) {
  return a < b ? b : a;
}

// 最大の黒クラスターが短辺を塞がなければ迂回なしで良い？
Tri solve(Tri a, Tri b, Tri c, Tri d){
  if(abs(c-a) > abs(d-b)){
    return solve(b, a, d, c);
  }
  dump(a.digit);
  dump(b.digit);
  dump(c.digit);
  dump(d.digit);
  repb(i, 30){
    if(a[i] == 1 && c[i] == 1){
      dump(i, b[i], d[i], abs(d-b), pow(Tri(3), i));
      if((b[i] == d[i] && abs(d-b) >= pow(Tri(3), i))
      || (min(b, d)[i] == 0 && max(b, d)[i] == 2)
      || (min(b, d)[i] == 2 && max(b, d)[i] == 0 && abs(d-b) >= Tri(2)*pow(Tri(3), i))){
        dump(i);
        Tri mi = a, ma = a;
        ll j = i;
        mi[j] = 0;
        ma[j] = 2;
        while(j){
          --j;
          mi[j] = 2;
          ma[j] = 0;
        }
        mi.reset_inv();
        ma.reset_inv();
        return abs(d-b) + min(a+c-mi*Tri(2), ma*Tri(2)-a-c);
      }
    }
    if(a[i] != c[i]) break;
  }
  return abs(d-b) + abs(c-a);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll q;
  cin >> q;
  while(q--){
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a--; b--; c--; d--;
    print(solve(Tri(a), Tri(b), Tri(c), Tri(d)));
  }
}


Measured memory_bytes: 384