#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using P = pair; int naive(int a, int b, int c, int d){ auto dist = vec2d(21, 21, -1); queue

que; auto set_dist = [&](int i, int j, int d){ if(i == j) return; if(i < 0 || i > 20 || j < 0 || j > 20) return; if(dist[i][j] == -1){ que.push(P(i, j)); dist[i][j] = d; } }; set_dist(a, b, 0); while(!que.empty()){ auto [i, j] = que.front(); que.pop(); set_dist(i+1, j, dist[i][j]+1); set_dist(i, j+1, dist[i][j]+1); set_dist(i, 0, dist[i][j]+1); set_dist(0, j, dist[i][j]+1); } return dist[c][d]; } bool ok(int a, int b, int c, int d){ if(a > b){ swap(a, b); swap(c, d); } if(c >= a && d >= b && d > c){ return true; } return false; } ll solve(ll a, ll b, ll c, ll d){ if(a > b){ swap(a, b); swap(c, d); } if(c < a && d < b){ return c+d+2; } if(c >= a && d >= b && d > c){ return c-a+d-b; } if(ok(0, b, c, d)){ return c+d-b+1; } if(ok(a, 0, c, d)){ return c+d-a+1; } if(a == 1) return c+d+3; return c+d+2; } void test(){ for(int a = 1; a <= 10; a++){ for(int b = 1; b <= 10; b++){ if(a == b) continue; for(int c = 1; c <= 10; c++){ for(int d = 1; d <= 10; d++){ if(c == d) continue; int ans0 = naive(a, b, c, d); int ans1 = solve(a, b, c, d); // if(ans1 == -1) continue; if(ans0 == ans1) continue; cout << a << ' ' << b << ' ' << c << ' ' << d << ':' << ans0 << ' ' << ans1 << endl; } } } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; // test(); int t; cin >> t; while(t--) { ll a, b, c, d; cin >> a >> b >> c >> d; cout << solve(a, b, c, d) << endl; } }