#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

// dice<hoge> di(arr); ÈÇÆé¾·é
// ±ÌÆ«AarrÍ hoge di[6];Åé¾³êÄ¨èAid ÌlÌÉÊÌlªi[³êÄ¢éKvª éB
// á¦ÎAid[TOP] = 3 ÈçÎAdi[3] ÌlªTOPÉÝè³êéB
// TCRÆµÄµ¢©Ç¤©Ì»èÉ equivalent_to ðg¤B
enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
template <class T>
class dice {
public:
  dice(T *v) {
    id[TOP] = 0; id[FRONT] = 1; id[LEFT] = 2;
    id[RIGHT] = 3; id[BACK] = 4; id[BOTTOM] = 5;
    REP(i,6)
      var[i] = v[i];
  }
  T& operator[] (FACE f) { return var[id[f]]; }
  const T& operator[] (FACE f) const { return var[id[f]]; }
  bool operator==(const dice<T>& b) const {
    const dice<T> &a = *this;
    return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&
           a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&
           a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
  }
  void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }
  void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }
  void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }
  vector<dice> all_rolls() {
    vector<dice> ret;
    for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        ret.push_back(*this);
    return ret;
  }
  bool equivalent_to(const dice& di) {
    for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        if (*this == di) return true;
    return false;
  }
private:
  void roll(FACE a, FACE b, FACE c, FACE d) {
    int tmp = id[a];
    id[a] = id[b]; id[b] = id[c];
    id[c] = id[d]; id[d] = tmp;
  }
  T var[6];
  int id[6];
};


int ba[5][5];
int tmpba[5][5];
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};

bool used[6];

bool solve(int x, int y, dice<int> di) {
  if (used[di[BOTTOM]-1]) return false;
  used[di[BOTTOM]-1] = 1;
  tmpba[x][y] = 0;

  //printf("%d, %d\n", x,y);

  REP(k,4) {
    int xx = x+dx[k];
    int yy = y+dy[k];
    if (xx<0||xx>=5||yy<0||yy>=5) continue;
    dice<int> tmp = di;
    if (k == 0) { tmp.roll_x();tmp.roll_x();tmp.roll_x(); }
    else if (k == 1) tmp.roll_y();
    else if (k == 2) tmp.roll_x();
    else { tmp.roll_y();tmp.roll_y();tmp.roll_y(); }
    
    if (tmpba[xx][yy] != 0){
      // cout << k << " " << xx << ", " << yy << " ";
      //     cout << tmp[BOTTOM] << endl;
      if (tmp[BOTTOM] == tmpba[xx][yy]) {
        if (!solve(xx,yy,tmp)) {
          return false;
        }
      } else
        return false;
    }
  }
  return true;
}

int main() {
  int N;
  cin >> N;
  while(N--) {
    int sx = -1, sy;
    int p;
    REP(y,5) {
      REP(x,5) {
        cin >> ba[x][y];
        if (ba[x][y] != 0 && sx < 0) {
          sx = x;
          sy = y;
          p = ba[x][y];
        }
      }
    }
    if (sx < 0) {
      cout << "false" <<endl;
      continue;
    }
    int arr[] = {1,2,3,4,5,6};
    bool f = 0;
    do {
      dice<int> di(arr);
      if (di[BOTTOM] + di[TOP] != 7 || di[FRONT] + di[BACK] != 7 || di[LEFT] + di[RIGHT] != 7) continue;
      if (di[BOTTOM] == p) {
        // cout << "p = " << p << endl;
        // cout << "front = " << di[FRONT] << endl;
        memset(used,0,sizeof(used));
        memcpy(tmpba, ba, sizeof(ba));
        if (solve(sx, sy, di)) {
          f = 1;
          REP(i,6) {
            if (!used[i])
              f = 0;
          }
          REP(y,5) REP(x,5)
            if (tmpba[x][y])
              f = 0;
          if (f) break;
        }
      }
    } while(next_permutation(arr, arr+6));
    if (f)
      cout << "true" << endl;
    else
      cout << "false" << endl;
  }
}

Measured memory_bytes: 0