#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while(tt--) { int a, b, c, d; cin >> a >> b >> c >> d; if (c > d) swap(a, b), swap(c, d); map dp; priority_queue, vector>, greater>> q; constexpr long long INF = 1002003004005006007; ll ans = INF; auto dist = [&](P x, P y) { return abs(x.first - y.first) + abs(x.second - y.second); }; auto add = [&](P p, ll v) { auto [x, y] = p; if (x == y) return; if (x < y && x <= c && y <= d) { chmin(ans, v + dist(p, {c, d})); } if (dist(p, {a, b}) > 7 && dist(p, {a, 0}) > 7 && dist(p, {0, b}) > 7 && dist(p, {0, 0}) > 7) return; auto it = dp.find({x, y}); if (it != dp.end() && it->second <= v) return; dp[{x, y}] = v; q.emplace(v, P(x, y)); }; add({a, b}, 0); while(q.size()) { auto [v, p] = q.top(); q.pop(); if (dp[p] != v) continue; auto [x, y] = p; add({x + 1, y}, v + 1); add({x, y + 1}, v + 1); add({0, y}, v + 1); add({x, 0}, v + 1); } cout << ans << '\n'; } }