#include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstring>
using namespace std;
int main(){
  int i,j,k,l,m;
  int n;
  cin>>n;
  for(i=0;i<n;++i){
    int a[7][7],b[6]={};
    memset(a,-1,sizeof(a));
    for(j=1;j<=5;++j){
      for(k=1;k<=5;++k){
	cin>>a[j][k];
	if(a[j][k])
	  b[a[j][k]-1]++;
      }
    }
    for(j=0;j<6;++j){
      if(b[j]!=1)
	break;
    }
    if(j<6){
      cout<<"false"<<endl;
    }else{
      int c[7][7];
      memset(c,-1,sizeof(c));
      for(j=1;j<=5;++j){
	for(k=1;k<=5;++k){
	  if(a[j][k]&&c[j][k]){
	    int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
	    for(l=0;l<4;++l){
	      for(m=0;;++m){
		if(a[j+dy[l]+dx[l]*m][k+dx[l]+dy[l]*m]>0){
		  if(a[j+dy[l]*2+dx[l]*m][k+dx[l]*2+dy[l]*m]>0&&c[j+dy[l]*2+dx[l]*m][k+dx[l]*2+dy[l]*m]&&a[j][k]+a[j+dy[l]*2+dx[l]*m][k+dx[l]*2+dy[l]*m]==7){
		    c[j][k]=c[j+dy[l]*2+dx[l]*m][k+dx[l]*2+dy[l]*m]=0;
		    break;
		  }
		}else{
		  break;
		}
	      }
	      if(c[j][k]==0)
		break;
	      for(m=0;;++m){
		if(a[j+dy[l]-dx[l]*m][k+dx[l]-dy[l]*m]>0){
		  if(a[j+dy[l]*2-dx[l]*m][k+dx[l]*2-dy[l]*m]>0&&c[j+dy[l]*2-dx[l]*m][k+dx[l]*2-dy[l]*m]&&a[j][k]+a[j+dy[l]*2-dx[l]*m][k+dx[l]*2-dy[l]*m]==7){
		    c[j][k]=c[j+dy[l]*2-dx[l]*m][k+dx[l]*2-dy[l]*m]=0;
		    break;
		  }
		}else{
		  break;
		}
	      }
	      if(c[j][k]==0)
		break;
	    }
	    if(l==4)
	      break;
	  }
	}
	if(k<=5)
	  break;
      }
      if(j>5)
	cout<<"true"<<endl;
      else
	cout<<"false"<<endl;
    }
  }
  return 0;
}