#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;


// If this structure is used for two-dimensional surface,
// it assumes that the face id[0] faces the positive direction of the Y axis,
// the face id[1] faces the positive direction of the X axis
// and id[2] is top of the dice.
struct Dice {
  int id[6], cnt, x, y;
  const static int f[3][3];

  Dice() { init(); }

  void init() {
    for(int i = 0; i < 6; ++i) {
      id[i] = i;
    }
    cnt = 0;
  }

  // type (0:spin, 1:rollX, 2:rollY)
  // dir 1( ccw     neg      pos   )
  //    -1( cw      pos      neg   )
  void rotate(int type, int dir = 1) {
    const int *v = f[type];
    int a,b,c,t; a=v[0]; b=v[1]; c=v[2]; t=id[type];
    if(dir < 0) swap(a,c);
    id[type]=id[a]; id[a]=id[b]; id[b]=id[c]; id[c]=t;
  }

  bool next() {
    ++cnt;
    if(cnt == 24) {
      rotate(0,-1);
      rotate(1,-1);
      cnt = 0;
      return false;
    }
    if(cnt == 4*3) {
      rotate(2,-1);
      rotate(0,-1);
    } else if(cnt%4 == 0) {
      rotate(2);
    } else {
      rotate(0);
    }
    return true;
  }

  void roll(int dir) {
    //const static int dx[] = {1,0,-1,0};
    //const static int dy[] = {0,1,0,-1};
    switch(dir) {
      case 0: rotate(1,-1); break;
      case 1: rotate(2, 1); break;
      case 2: rotate(1, 1); break;
      case 3: rotate(2,-1); break;
    }
  }

  bool operator < (const Dice &d) const {
    for(int i = 0; i < 3; ++i)
      if(id[i] != d.id[i]) return id[i] < d.id[i];
    return false;
  }

  void show() {
    cout << id[4] << " / \\ " << id[5] << endl;
    cout << " /   \\" << endl;
    cout << "|\\ " << id[2] << " /|" << endl;
    cout << "|" << id[0] << "\\ /" << id[1] << "|" << endl;
    cout << " \\ | /" << endl;
    cout << "  \\|/" << endl;
    cout << "   " << id[3] << endl;
  }
};
const int Dice::f[3][3] = {{4,5,1},{3,4,2},{5,3,0}};



const int H = 5;
const int W = 5;
const int di[] = {0,1,0,-1};
const int dj[] = {1,0,-1,0};

char G[H][W];

bool check_g() {
  int sum = 0;
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if(G[i][j] != '0') ++sum;
    }
  }
  return sum == 6;
}

bool check_b() {
  set<char> s;
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if(G[i][j] != '0') s.insert(G[i][j]);
    }
  }
  return s.size() == 6;
}

bool check_ef() {
  bool vis[H][W];
  int cnt = 0;
  fill(vis[0], vis[H], false);
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if(G[i][j] != '0' && !vis[i][j]) {
        if(++cnt != 1) return false;
        queue<pair<int,int> > que;
        que.push(make_pair(i,j));
        vis[i][j] = true;


        while(!que.empty()) {
          int ni = que.front().first;
          int nj = que.front().second;
          que.pop();
          for(int k = 0; k < 4; ++k) {
            int nni = ni + di[k];
            int nnj = nj + dj[k];
            if(nni < 0 || nni >= H) continue;
            if(nnj < 0 || nnj >= W) continue;
            if(G[nni][nnj] == '0') continue;
            if(vis[nni][nnj]) continue;
            vis[nni][nnj] = true;
            que.push(make_pair(nni,nnj));
          }
        }


      }
    }
  }
  return true;
}

bool check_acd() {
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if(G[i][j] != '0') {
        queue<Dice> que;
        bool vis[H][W];
        Dice d;
        int num[6];
        fill(num, num+6, -1);
        d.y = i;
        d.x = j;
        fill(vis[0], vis[H], false);
        vis[i][j] = true;
        que.push(d);
        num[d.id[3]] = G[d.y][d.x] - '0';
        while(!que.empty()) {
          d = que.front();
          que.pop();
          for(int k = 0; k < 4; ++k) {
            Dice nd = d;
            nd.x += dj[k];
            nd.y += di[k];
            nd.roll(k);
            if(nd.x < 0 || nd.x >= W) continue;
            if(nd.y < 0 || nd.y >= H) continue;
            if(G[nd.y][nd.x] == '0') continue;
            if(vis[nd.y][nd.x]) continue;
            if(num[nd.id[3]] != -1) return false;
            num[nd.id[3]] = G[nd.y][nd.x] - '0';
            vis[nd.y][nd.x] = true;
            que.push(nd);
          }
        }

        for(int i = 0; i < 6; ++i) {
          if(num[i] + num[5-i] != 7) return false;
        }

        return true;
      }
    }
  }
  return false;
}

bool solve() {
  if(!check_g()) return false;
  if(!check_b()) return false;
  if(!check_ef()) return false;
  if(!check_acd()) return false;
  return true;
}

int main() {
  int N;
  cin >> N;
  while(N--) {
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
        cin >> G[i][j];
      }
    }

    cout << (solve() ? "true" : "false") << endl;

  }
  return 0;
}

Measured memory_bytes: 1196