#include #include using namespace std; using ll = long long; using ld = long double; using mint = atcoder::modint1000000007; const ll inf = 9e18; bool in_out(ll x,ll y,ll h,ll w){ return 0<=x and x> h >> w; vector a(h,vector(w)); for(ll i=0;i> a[i][j]; } } ll Q; cin >> Q; while(Q--){ ll si,sj,gi,gj; cin >> si >> sj >> gi >> gj; si--;sj--;gi--;gj--; ll ok=h*w+1,ng=0; while(ok-ng>1){ ll x=(ok+ng)/2; vector vis(h,vector(w,false)); if(a[si][sj]<=x){ vis[si][sj]=true; queue> q; q.push({si,sj}); while(q.size()){ auto[i,j]=q.front(); q.pop(); for(ll d=0;d<4;d++){ ll ni=i+di[d]; ll nj=j+dj[d]; if(in_out(ni,nj,h,w)==false) continue; if(a[ni][nj]>x) continue; if(vis[ni][nj]==true) continue; vis[ni][nj]=true; q.push({ni,nj}); } } } if(vis[gi][gj]==false) ng=x; else ok=x; } cout << ok << endl; } }