/* -*- coding: utf-8 -*- * * 2213.cc: No.2213 Neq Move - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_M = 4 + 2; const int MAX_N = MAX_M * MAX_M; const int INF = 1 << 30; const long long LINF = 1LL << 62; /* typedef */ using ll = long long; using pli = pair; /* global variables */ int es[MAX_N][MAX_N]; ll ds[MAX_N]; /* subroutines */ /* main */ int main() { int tn; scanf("%d", &tn); while (tn--) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); // a->c, b->d int as[] = { 0, 1, a, b, c, d }; sort(as, as + 6); int m = unique(as, as + 6) - as; int n = m * m; int st = -1, gl = -1; for (int u = 0; u < n; u++) { fill(es[u], es[u] + n, INF); es[u][u] = 0; int ux = u / m, uy = u % m; if (as[ux] == a && as[uy] == b) st = u; if (as[ux] == c && as[uy] == d) gl = u; if (ux == uy) continue; for (int v = 0; v < n; v++) { int vx = v / m, vy = v % m; if (u == v || vx == vy) continue; if (ux == vx) { if (uy < vy && (ux < uy || vy < ux)) es[u][v] = as[vy] - as[uy]; else if (vy == 0) es[u][v] = 1; } if (uy == vy) { if (ux < vx && (uy < ux || vx < uy)) es[u][v] = as[vx] - as[ux]; else if (vx == 0) es[u][v] = 1; } } } //for (int i = 0; i < m; i++) printf(" %d", as[i]); putchar('\n'); //printf(" st=%d, gl=%d\n", st, gl); fill(ds, ds + n, LINF); ds[st] = 0; priority_queue q; q.push({0, st}); while (! q.empty()) { auto [ud, u] = q.top(); q.pop(); ud = -ud; if (ds[u] != ud) continue; for (int v = 0; v < n; v++) if (es[u][v] < INF) { ll vd = ud + es[u][v]; if (ds[v] > vd) { ds[v] = vd; q.push({-vd, v}); } } } printf("%lld\n", ds[gl]); } return 0; }