#ifndef hari64 #include // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #define debug(...) #else #include "util/viewer.hpp" #define debug(...) viewer::_debug(__LINE__, #__VA_ARGS__, __VA_ARGS__) #endif using namespace std; constexpr int INF = 1001001001; constexpr long long INFll = 1001001001001001001; template bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template bool chmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } using S = tuple>; int main() { cin.tie(0); ios::sync_with_stdio(false); int T; cin >> T; while (T--) { long long A, B, C, D; cin >> A >> B >> C >> D; set seen; priority_queue, greater> q; q.push({0, 0, {A, B, C, D}}); while (!q.empty()) { auto [val, cnt, abcd] = q.top(); q.pop(); if (seen.find({val, cnt, abcd}) != seen.end()) { continue; } seen.insert({val, cnt, abcd}); // debug(val, cnt, abcd); auto [a, b, c, d] = abcd; if (a == c && b == d) { cout << val << endl; break; } if (a + 1 < b && a == 0) { q.push({val + 1, cnt, {a + 1, b, c, d}}); } if (a < b - 1) { q.push({val + b - 1 - a, cnt, {b - 1, b, c, d}}); } if (b + 1 < a && b == 0) { q.push({val + 1, cnt, {a, b + 1, c, d}}); } if (b < a - 1) { q.push({val + a - 1 - b, cnt, {a, a - 1, c, d}}); } if (a < c && !(a <= b && b <= c)) { q.push({val + c - a, cnt, {c, b, c, d}}); } if (b < d && !(b <= a && a <= d)) { q.push({val + d - b, cnt, {a, d, c, d}}); } if (a != 0 && b != 0 && cnt < 10) { q.push({val + 1, cnt + 1, {0, b, c, d}}); q.push({val + 1, cnt + 1, {a, 0, c, d}}); } } } return 0; }