#include<bits/stdc++.h>
using namespace std;
char g[6][6];
char check[6][6]={
'0','0','0','0','0','0',
'0','1','1','1','1','1',
'0','0','1','1','1','1',
'0','0','0','*','1','1',
'0','0','0','0','0','1',
'0','0','0','0','0','0',
};
int dx[9]={0,-2,-2,-1,-1,1,1,2,2};
int dy[9]={0,-1,1,-2,2,-2,2,-1,1};
int maxdepth=1;
int judge()
{
int cnt=0;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(g[i][j]!=check[i][j]) cnt++;
}
}
return cnt;
}
bool dfs(int x,int y,int pre,int depth)
{
int l=judge();
if(l==0) return true;
if(l+depth-1>maxdepth) return false;
for(int i=1;i<=8;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(xx<1||xx>5||yy<1||yy>5||(pre+i==9)) continue;
swap(g[x][y],g[xx][yy]);
if(dfs(xx,yy,i,depth+1)) return true;
swap(g[x][y],g[xx][yy]);
}
return false;
}
int main()
{
int t=0;
cin>>t;
while(t--)
{
int flag=1;
int x=0,y=0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
cin>>g[i][j];
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
{
if(g[i][j]=='*') x=i,y=j;
}
while(maxdepth++&&flag==1)
{
if(dfs(x,y,0,0))
{
if(maxdepth>=15)
cout<<"-1"<<endl;
else
cout<<maxdepth<<endl;
flag=0;
}
}
maxdepth=1;
}
return 0;
}