#include <iostream>
#include <vector>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll solve(ll ai, ll aj, ll bi, ll bj) {
    if (aj > bj) {
        swap(ai, bi);
        swap(aj, bj);
    }

    ll s = 1;
    rep(i,30) s *= 3;
    ll dist = abs(ai-bi) + abs(aj-bj);
    ll res = dist;
    while(1) {
        s /= 3;
        if (s <= 0) break;

        ll nai = ai / s, nbi = bi / s;
        if (nai != nbi) break;

        ll naj = aj / s, nbj = bj / s;
        if (nai % 3 == 1 && abs(naj-nbj) > 1) {
            ll up = s*nai-1;
            ll bot = s*(nai+1);
            ll loss = min(ai,bi) - up;
            loss = min(loss, bot - max(ai, bi));
            res = dist + loss * 2;
            break;
        }
    }
    return res;
}

int main() {
    int q;
    cin >> q;

    rep(qi, q) {
        ll ai, aj, bi, bj;
        cin >> ai >> aj >> bi >> bj;
        ai--; aj--; bi--; bj--;

        ll ans = 0;
        ans = max(ans, solve(ai,aj,bi,bj));
        ans = max(ans, solve(aj,ai,bj,bi));
        cout << ans << endl;
    }

    return 0;
}