#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
int dxx[]= {1,-1,0,0};
int dyy[]= {0,0,-1,1};
int a[9][9] = {
{ 1,1,1,1,1,1,1,1,1 },
{ 1,0,0,1,0,0,1,0,1 },
{ 1,0,0,1,1,0,0,0,1 },
{ 1,0,1,0,1,1,0,1,1 },
{ 1,0,0,0,0,1,0,0,1 },
{ 1,1,0,1,0,1,0,0,1 },
{ 1,1,0,1,0,1,0,0,1 },
{ 1,1,0,1,0,0,0,0,1 },
{ 1,1,1,1,1,1,1,1,1 }
};
int r,b,c,d,ans=-1;
struct point
{
int x,y,s;
point(int xx,int yy,int ss)
{
x=xx;
y=yy;
s=ss;
}
};
void bfs(int x,int y,int s)
{
queue<point> q;
q.push(point(x,y,s));
while(!q.empty())
{
point dp=q.front();
q.pop();
int dx=dp.x;
int dy=dp.y;
int ds=dp.s;
if(dx==c&dy==d)
{
ans=ds;
return;
}
for(int i=0; i<4; i++)
{
int nx=dx+dxx[i];
int ny=dy+dyy[i];
if(nx<0||ny<0||nx>=9||ny>=9||a[nx][ny]==1)
continue;
a[nx][ny]=1;
q.push(point(nx,ny,ds+1));
}
}
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d",&r,&b,&c,&d);
bfs(r,b,0);
printf("%d\n",ans);
}
}
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
int dxx[]= {1,-1,0,0};
int dyy[]= {0,0,-1,1};
int a[9][9] = {
{ 1,1,1,1,1,1,1,1,1 },
{ 1,0,0,1,0,0,1,0,1 },
{ 1,0,0,1,1,0,0,0,1 },
{ 1,0,1,0,1,1,0,1,1 },
{ 1,0,0,0,0,1,0,0,1 },
{ 1,1,0,1,0,1,0,0,1 },
{ 1,1,0,1,0,1,0,0,1 },
{ 1,1,0,1,0,0,0,0,1 },
{ 1,1,1,1,1,1,1,1,1 }
};
int r,b,c,d,ans=-1;
struct point
{
int x,y,s;
point(int xx,int yy,int ss)
{
x=xx;
y=yy;
s=ss;
}
};
void bfs(int x,int y,int s)
{
queue<point> q;
q.push(point(x,y,s));
while(!q.empty())
{
point dp=q.front();
q.pop();
int dx=dp.x;
int dy=dp.y;
int ds=dp.s;
if(dx==c&dy==d)
{
ans=ds;
return;
}
for(int i=0; i<4; i++)
{
int nx=dx+dxx[i];
int ny=dy+dyy[i];
if(nx<0||ny<0||nx>=9||ny>=9||a[nx][ny]==1)
continue;
a[nx][ny]=1;
q.push(point(nx,ny,ds+1));
}
}
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d",&r,&b,&c,&d);
bfs(r,b,0);
printf("%d\n",ans);
}
}