#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL<<60;
typedef pair<ll, ll> P;

vec san(32, 1);

int bit3_t(ll a, int t){
    return (a % san[t + 1]) / san[t];
}

bool between(ll x, ll y1, ll y2){
    if(y1 > y2) swap(y1, y2);
    ++y1; --y2;
    if(y1 > y2) return false;
    while(x > 0){
        if(x%3 == 1){
            if(y2 - y1 > 3) return true;
            for(ll y = y1; y <= y2; ++y) if(y%3 == 1) return true;
        }
        x /= 3;
        y1 /= 3;
        y2 /= 3;
    }
    return false;
}

ll f(ll a, ll b, ll c, ll d){
    ll res = 0;
    Rrep(i, 31){
        /*if(bit3_t(b, i) == 1 && bit3_t(d, i) == 1){
            swap(a, b);
            swap(c, d);
        }*/
        if(bit3_t(a, i) == 1 && bit3_t(c, i) == 1 && a / san[i] == c / san[i]){
            if(abs((d / san[i]) - (b / san[i])) >= 2){
            //if(between(a / san[i], b / san[i], d / san[i])){
                ll diff1 = a % san[i], diff2 = c % san[i];
                res = max(res, min({diff1 + 1, san[i] - diff1,
                                    diff2 + 1, san[i] - diff2}) * 2);
            }
        }
    }
    return res;
}


int main() {
    cin>>Q;
    reps(i, 1, 32) san[i] = san[i-1] * 3;
    rep(i,Q){
        vec v(4);
        rep(j,4){
            cin>>v[j];
            --v[j];
        }
        ll res = abs(v[0] - v[2]) + abs(v[1] - v[3]);
        //cout<<res<<endl;
        cout<<res + max(f(v[0], v[1], v[2], v[3]), f(v[1], v[0], v[3], v[2]))<<endl;
    }
}