#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it,c) for(__typeof((c).begin())it=(c).begin(); it!=(c).end();++it)
#define RFOR(it,c) for(__typeof((c).rbegin())it=(c).rbegin(); it!=(c).rend();++it)
#define exist(c, v) (find((c).begin(), (c).end(), (v)) != (c).end())

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
	return os;
}

enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
class dice {
private:
	int num[6];

	void roll(FACE a, FACE b, FACE c, FACE d) {
		int tmp = num[a];
		num[a] = num[b];
		num[b] = num[c];
		num[c] = num[d];
		num[d] = tmp;
	}

public:
	dice() {
		memset(num, 0, sizeof(num));
	}

	bool check() {
		vector<bool> used(7, false);
		for(int i = 0; i < 6; ++i) {
			if(num[i] == 0 || used[num[i]])
				return false;
			used[num[i]] = true;
		}

		return num[TOP] + num[BOTTOM] == 7 && num[FRONT] + num[BACK] == 7 && num[LEFT] + num[RIGHT] == 7;
	}

	int& operator[](FACE f) { return num[f]; }
	void north() { roll(TOP, BACK, BOTTOM, FRONT); }
	void south() { roll(TOP, FRONT, BOTTOM, BACK); }
	void east() { roll(TOP, LEFT, BOTTOM, RIGHT); }
	void west() { roll(TOP, RIGHT, BOTTOM, LEFT); }
};

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

dice die;
int f[5][5];
bool visited[5][5];

void search(int x, int y) {
	die[TOP] = f[y][x];
	visited[y][x] = true;

	for(int i = 0; i < 4; ++i) {
		const int nx = x + dx[i], ny = y + dy[i];
		if(nx < 0 || ny < 0 || nx >= 5 || ny >= 5)
			continue;

		if(visited[ny][nx] || !f[ny][nx])
			continue;

		switch(i) {
		case 0: die.east(); break;
		case 1: die.west(); break;
		case 2: die.south(); break;
		case 3: die.north(); break;
		}

		search(nx, ny);

		switch(i) {
		case 0: die.west(); break;
		case 1: die.east(); break;
		case 2: die.north(); break;
		case 3: die.south(); break;
		}
	}
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	while(n--) {
		for(int i = 0; i < 5; ++i)
			for(int j = 0; j < 5; ++j)
				cin >> f[i][j];

		bool ok = false, first = true;
		int cnt = 0;
		for(int i = 0; i < 5; ++i)
			for(int j = 0; j < 5; ++j)
				if(f[i][j])
					++cnt;

		if(cnt != 6)
			goto finish;

		die = dice();
		memset(visited, false, sizeof(visited));
		for(int i = 0; i < 5; ++i) {
			for(int j = 0; j < 5; ++j) {
				if(f[i][j] && !visited[i][j]) {
					if(!first)
						goto finish;

					first = true;
					search(j, i);
				}
			}
		}

		ok = die.check();

	finish:;
		cout << (ok ? "true" : "false") << endl;
	}

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1196