#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> roll_i(vector<int> dice, int a, int b, int c, int d){
  vector<int> res = dice;
  res[a] = dice[b];
  res[b] = dice[c];
  res[c] = dice[d];
  res[d] = dice[a];
  return res;
}
vector<int> roll(vector<int> d, int r){
  if(r == 0){
    return roll_i(d, 0, 1, 3, 4);
  }else if(r == 1){
    return roll_i(d, 0, 2, 3, 5);
  }else if(r == 2){
    return roll_i(d, 0, 4, 3, 1);
  }else if(r == 3){
    return roll_i(d, 0, 5, 3, 2);
  }
}
bool solve(int mesh[5][5]){
  bool used[5][5] = {};
  int f_face;
  int sx, sy;
  REP(y, 5)REP(x, 5)if(mesh[y][x]){
    f_face = mesh[y][x];
    sx = x, sy = y;
    queue<int> qx, qy;
    qx.push(x); qy.push(y);
    while(!qx.empty()){
      int x = qx.front(); qx.pop();
      int y = qy.front(); qy.pop();
      used[y][x] = true;
      REP(r, 4){
        int nx = x + dx[r];
        int ny = y + dy[r];
        if(valid(nx, ny, 5, 5) && !used[ny][nx] && mesh[ny][nx]){
          qx.push(nx); qy.push(ny);
        }
      }
    }
    goto END;
  }
END:
  int cnt[6] = {};
  REP(y, 5)REP(x, 5){
    if(used[y][x]){
      cnt[mesh[y][x] - 1] ++;
    }else if(mesh[y][x]){
      return false;
    }
  }
  REP(i, 6) if(cnt[i] != 1) return false;
  vector<int> dice_rest;
  for(int i = 1; i <= 6; i++){
    if(f_face != i && f_face + i != 7){
      dice_rest.push_back(i);
    }
  }
  do{
    bool ok = true;
    if(dice_rest[0] + dice_rest[2] != 7) continue;
    vector<int> dice(6);
    dice[0] = f_face; dice[1] = dice_rest[0]; dice[2] = dice_rest[1];
    dice[3] = 7 - f_face; dice[4] = dice_rest[2]; dice[5] = dice_rest[3];
    queue<vector<int> > qd;
    queue<int> qx, qy;
    bool used2[5][5] = {};
    qx.push(sx);
    qy.push(sy);
    qd.push(dice);
    /*
    int C = 0;
    printf("start:");
    debug(dice.begin(), dice.end());
    */
    while(!qx.empty()){
      int x = qx.front(); qx.pop();
      int y = qy.front(); qy.pop();
      vector<int> d = qd.front(); qd.pop();
      /*
      printf("%d: ", mesh[y][x]);
      debug(d.begin(), d.end());
      */
      if(d[0] != mesh[y][x]){
        ok = false;
        break;
      }
      used2[y][x] = true;
      REP(r, 4){
        int nx = x + dx[r];
        int ny = y + dy[r];
        vector<int> nd = roll(d, r);
        if(valid(nx, ny, 5, 5) && !used2[ny][nx] && mesh[ny][nx]){
          qx.push(nx); qy.push(ny); qd.push(nd);
        }
      }
    }
    if(ok) return true;
  }while(next_permutation(dice_rest.begin(), dice_rest.end()));
  return false;
}

int main(){
  int N;
  cin>>N;
  while(N--){
    int mesh[5][5];
    REP(i, 5)REP(j, 5) cin>>mesh[i][j];
    if(solve(mesh)) cout<<"true"<<endl;
    else cout<<"false"<<endl;
  }
  return 0;
}

Measured memory_bytes: 1220