#include <iostream>

using lint = long long;

void solve() {
    lint x1, y1, x2, y2;
    std::cin >> x1 >> y1 >> x2 >> y2;
    --x1, --y1, --x2, --y2;

    lint ans = std::abs(x2 - x1) + std::abs(y2 - y1);
    lint three = 1;

    for (int i = 0; i < 30; ++i) {
        for (int j = 0; j < 2; ++j) {
            std::swap(x1, y1);
            std::swap(x2, y2);

            if (x1 / three != x2 / three ||
                (x1 / three) % 3 != 1 ||
                std::abs(y1 / three - y2 / three) <= 1) continue;

            lint d1 = x1 % three, d2 = x2 % three;
            lint d = std::min(d1 + d2 + 2, three - d1 + three - d2);
            ans = std::max(ans, d + std::abs(y2 - y1));
        }
        three *= 3;
    }

    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int q;
    std::cin >> q;
    while (q--) solve();

    return 0;
}
