#include #include #include #include #include #include using namespace std; typedef long long LL; int T; char mp[6][6]; const char end_status[6][6]={ "11111", "01111", "00*11", "00001", "00000", " " }; int depth_limit; int Evaluate() { int cnt=0; for(int i=0;i<5;i++) for(int j=0;j<5;j++) if(mp[i][j]!=end_status[i][j]) cnt++; return cnt-1; } bool dfs(int dep,int ur,int uc) { int g=Evaluate(); if(dep+g>depth_limit) return 0; if(g==-1) return 1; const int dr[]={-2,-2,-1,1,2,2,1,-1}; const int dc[]={-1,1,2,2,1,-1,-2,-2}; for(int i=0;i<8;i++) { int vr=ur+dr[i],vc=uc+dc[i]; if(vr<0||vr>=5||vc<0||vc>=5) continue; swap(mp[ur][uc],mp[vr][vc]); if(dfs(dep+1,vr,vc)) return 1; swap(mp[ur][uc],mp[vr][vc]); } return 0; } int main() { #ifdef local freopen("pro.in","r",stdin); #endif scanf("%d",&T); while(T-->0) { for(int i=0;i<5;i++) scanf("%s",mp[i]); int ur=-1,uc=-1; for(int i=0;i<5;i++) for(int j=0;j<5;j++) if(mp[i][j]=='*') { ur=i; uc=j; break; } assert(ur>=0&&uc>=0); for(depth_limit=0;depth_limit<=15;depth_limit++) if(dfs(0,ur,uc)) { printf("%d\n",depth_limit); goto nxt; } puts("-1"); nxt:; } return 0; }