#include <bits/stdc++.h>
using namespace std;

int a[7][7];
bool vis[7][7];
int ans[7];

int r[4][7] = {
	{0, 3, 2, 6, 4, 1, 5},
	{0, 4, 1, 3, 6, 5, 2},
	{0, 5, 2, 1, 4, 6, 3},
	{0, 2, 6, 3, 1, 5, 4}
};

int di[] = {0, -1, 0, 1};
int dj[] = {1, 0, -1, 0};

void dfs(int i, int j, vector <int> v) {
	vis[i][j] = 1;
	ans[v[1]] = a[i][j];
	/*
	cout << "------\n";
	cout << i << ' ' << j << '\n';
	for(int i = 1; i <= 6; i ++)
		cout << v[i] << ' ';
	cout << '\n';
	*/
	for(int k = 0; k < 4; k ++) {
		int ni = i + di[k];
		int nj = j + dj[k];
		vector <int> u(7);
		for(int t = 0; t < 7; t ++)
			u[t] = v[r[k][t]];
		if(!vis[ni][nj] && a[ni][nj])
			dfs(ni, nj, u);
	}
}

int main() {
	ios :: sync_with_stdio(false);
	int t; cin >> t;
	while(t --) {
		for(int i = 1; i <= 5; i ++)
			for(int j = 1; j <= 5; j ++)
				cin >> a[i][j];
		bool f = 1;
		for(int k = 1; k <= 6; k ++) {
			int c = 0;
			for(int i = 1; i <= 5; i ++)
				for(int j = 1; j <= 5; j ++)
					c += a[i][j] == k;
			f &= c == 1;
		}
		if(!f) {
			cout << "false\n";
			continue;
		}
		fill(ans, ans + 6, 0);
		memset(vis, 0, sizeof vis);
		for(int i = 1; i <= 5; i ++)
			for(int j = 1; j <= 5; j ++)
				if(a[i][j] == 1) {
					dfs(i, j, {0, 1, 2, 3, 4, 5, 6});
				}
		if(ans[1] + ans[6] == 7 && ans[2] + ans[4] == 7 && ans[3] + ans[5] == 7)
			cout << "true\n";
		else
			cout << "false\n";
	}
	return 0;
}