using namespace std;
int ma[10][10];
int f1[10][10];
int f2[10][10];
int f3[10][10];
struct asd{
int x,y;
int step;
};
asd q[N];
int head,tail;
int dx[8]={-2,-2,-1,-1,1,1,2,2};
int dy[8]={1,-1,2,-2,2,-2,1,-1};
void bfs1(int x,int y)
{
memset(f1,-1,sizeof(f1));
head=0;
tail=1;
q[head].x=x;
q[head].y=y;
f1[x][y]=0;
while(head<tail)
{
int a=q[head].x;
int b=q[head].y;
for(int i=0;i<8;i++)
{
int aa=a+dx[i];
int bb=b+dy[i];
if(aa<1||bb<1||aa>8||bb>8||f1[aa][bb]!=-1)
continue;
q[tail].step=q[head].step+1;
f1[aa][bb]=q[tail].step;
q[tail].x=aa;
q[tail].y=bb;
tail++;
}
head++;
}
}
void bfs2(int x,int y)
{
memset(f2,-1,sizeof(f2));
head=0;
tail=1;
q[head].x=x;
q[head].y=y;
f2[x][y]=0;
while(head<tail)
{
int a=q[head].x;
int b=q[head].y;
for(int i=0;i<8;i++)
{
int aa=a+dx[i];
int bb=b+dy[i];
if(aa<1||bb<1||aa>8||bb>8||f2[aa][bb]!=-1)
continue;
q[tail].step=q[head].step+1;
f2[aa][bb]=q[tail].step;
q[tail].x=aa;
q[tail].y=bb;
tail++;
}
head++;
}
}
void bfs3(int x,int y)
{
memset(f3,-1,sizeof(f3));
head=0;
tail=1;
q[head].x=x;
q[head].y=y;
f3[x][y]=0;
while(head<tail)
{
int a=q[head].x;
int b=q[head].y;
for(int i=0;i<8;i++)
{
int aa=a+dx[i];
int bb=b+dy[i];
if(aa<1||bb<1||aa>8||bb>8||f3[aa][bb]!=-1)
continue;
q[tail].step=q[head].step+1;
f3[aa][bb]=q[tail].step;
q[tail].x=aa;
q[tail].y=bb;
tail++;
}
head++;
}
}
int main()
{
int T;
scanf("%d",&T);
int x1,x3,x2,y1,y2,y3;
while(T--)
{
char a1[5];
char a2[5];
char a3[5];
scanf("%s%s%s",a1,a2,a3);
x1=a1[0]-'A'+1;
y1=a1[1]-'0';
x2=a2[0]-'A'+1;
y2=a2[1]-'0';
x3=a3[0]-'A'+1;
y3=a3[1]-'0';
bfs1(x1,y1);
bfs2(x2,y2);
bfs3(x3,y3);
int ans=1000;
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
ans=min(f1[i][j]+f2[i][j]+f3[i][j],ans);
}
}
printf("%d\n",ans);
}
return 0;
}