#include<iostream>
using namespace std;
#define BOTTOM 0
#define FRONT 1
#define RIGHT 2
#define TOP 3
#define BACK 4
#define LEFT 5

#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3
int table[6][6];
int inp[5][5];
int color[5][5];
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
bool visited[5][5];

void setting();
void dfs(int &cnt,int y,int x);
bool is_connect();

bool Construct(int now,int *used){
#ifdef DEBUG
  cout << "NOW STATE"<<endl;
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
      if (j)cout<<" ";
      if (color[i][j] != -1)cout<< color[i][j];
      else cout << " ";
    }
    cout << endl;
  }
  cout << "NOW STATE END " << endl;
#endif
  if ( now == 6){
    if ( used[TOP]+used[BOTTOM] == 7 && used[LEFT]+used[RIGHT]==7&&
	 used[FRONT]+used[BACK]==7)return true;
    else return false;
  }
  int y=-1,x=-1,mycolor;
  for(int i=0;i<5&&y == -1;i++){
    for(int j=0;j<5;j++){
      if ( color[i][j] != -1 && visited[i][j]==false){x=j;y=i;mycolor=color[i][j];break;}
    }
  }

  visited[y][x] = true;
  for(int base=0;base<4;base++){
    bool isvalid=true;
    int restore[6][2]={0},point=0;//for return back the data;
    int restore_used[6]={0};
    for(int i=0;i<4;i++){
      int nex=x+dx[i],ney=y+dy[i];
      if ( nex<0||ney<0||nex>=5||ney>5||inp[ney][nex]==0)continue;
      if ( color[ney][nex] == -1){
	if( used[table[mycolor][(base+i)%4]]!= 0){
	  isvalid=false;
	  break;
	}
	restore[point][0]=ney;
	restore[point][1]=nex;
	color[ney][nex]=table[mycolor][(base+i)%4];
	used[table[mycolor][(base+i)%4]]=inp[ney][nex];
	restore_used[table[mycolor][(base+i)%4]]=1;
	point++;
      }else if ( color[ney][nex] != table[mycolor][(base+i)%4]){
	isvalid=false;
	break;
      }
    }
    if ( isvalid == true && Construct(now+1,used))return true;
    for(int i=0;i<point;i++){
      int rx=restore[i][1],ry=restore[i][0];
      color[ry][rx]=-1;
    }
    for(int i=0;i<6;i++){
      if ( restore_used[i] == 1)used[i]=0;      
    }
    
  }
  visited[y][x]=false;
  return false;
}
bool solve(){
  int sx=-1,sy=-1;
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
      visited[i][j] = true;
      if ( inp[i][j] != 0&&sx==-1){
	sx=j;
	sy=i;
      }
    }
  }
  
  int used[6]={0};
  fill(&color[0][0],&color[4][5],-1);
  fill(&visited[0][0],&visited[4][5],false);
  used[BOTTOM]=inp[sy][sx];
  color[sy][sx]=BOTTOM;  
  if ( Construct(1,used))return true;
  else return false;
}


main(){
  setting();
  int te;
  cin>>te;
  while(te--){
    int isexist[7]={0},cnt=0;
    fill(&visited[0][0],&visited[4][5],false);
    for(int i=0;i<5;i++){
      for(int j=0;j<5;j++){
	cin>>inp[i][j];
	if ( inp[i][j] != 0 )cnt++,isexist[inp[i][j]]++;
      }
    }
    if ( cnt != 6||isexist[1]!=1||isexist[2]!=1||isexist[3]!=1||isexist[4]!=1||
	 isexist[5]!=1||isexist[6]!=1 || is_connect() ==false){
      cout << "false"<<endl;
      continue;
    }
#ifdef DEBUG
    cout << "connected and only distinct6values"<<endl;
#endif    
    if ( solve() == true){
      cout << "true"<<endl;
    }else cout << "false"<<endl;
  }
  
  return false;
}


void dfs(int &cnt,int y,int x){
  visited[y][x] = true;
  cnt++;
  for(int i=0;i<4;i++){
    int nex = x+dx[i],ney=y+dy[i];
    if ( nex<0||ney<0||nex>=5||ney>=5||inp[ney][nex]==0||visited[ney][nex])continue;
    dfs(cnt,ney,nex);
  }
}

bool is_connect(){
  int cnt = 0;
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
      if ( inp[i][j] != 0){
	dfs(cnt,i,j);
	if (cnt == 6)return true;
	else return false;
      }
    }
  }
  return false;
}



void setting(){
  table[BOTTOM][NORTH]=BACK;
  table[BOTTOM][EAST]=RIGHT;
  table[BOTTOM][SOUTH]=FRONT;
  table[BOTTOM][WEST]=LEFT;
  
  table[TOP][NORTH]=BACK;
  table[TOP][EAST]=LEFT;
  table[TOP][SOUTH]=FRONT;
  table[TOP][WEST]=RIGHT;

  table[FRONT][NORTH]=BOTTOM;
  table[FRONT][EAST]=RIGHT;
  table[FRONT][SOUTH]=TOP;
  table[FRONT][WEST]=LEFT;

  table[BACK][NORTH]=TOP;
  table[BACK][EAST]=RIGHT;
  table[BACK][SOUTH]=BOTTOM;
  table[BACK][WEST]=LEFT;

  table[RIGHT][NORTH]=BACK;
  table[RIGHT][EAST]=TOP;
  table[RIGHT][SOUTH]=FRONT;
  table[RIGHT][WEST]=BOTTOM;

  table[LEFT][NORTH]=BACK;
  table[LEFT][EAST]=BOTTOM;
  table[LEFT][SOUTH]=FRONT;
  table[LEFT][WEST]=TOP;
}

Measured memory_bytes: 0