#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;
void printArray (int arr[][9], int rowSize, int columnSize)
{
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < columnSize; j++)
{
cout << arr[i][j] <<" ";
}
cout<<endl;
}
}
void convertStringToInt (string line,int arr[][9], int rowSize, int whichRow )
{
int step = 0;
for(int i=0; i< line.length(); i++)
{
if ( !isspace(line[i]) )
{
arr[whichRow ][step] = line[i]-48;
//cout << arr[whichRow][step]<<" ";
step++;
}
}
}
bool checkRowColumnSum (int arr[][9], int rowSize, int columnSize, int criteriaForTrue)
{
bool verdict = true;
int sum = 0;
int i=0,goByRow=0, goByCol = 0;
for ( i = 0; i < rowSize; i++)
{
if (verdict == true)
{
for ( goByCol = 0; goByCol < columnSize; goByCol++)
{
sum+= arr[i][goByCol];
}
if (sum != criteriaForTrue)
{
verdict = false;
return verdict;
}
sum = 0;
for ( goByRow = 0; goByRow < rowSize; goByRow++)
{
sum+= arr[goByRow][i];
}
if (sum != criteriaForTrue)
{
verdict = false;
return verdict;
}
if (sum != criteriaForTrue)
{
verdict = false;
return verdict;
}
sum = 0;
}
else if (verdict == false)
return verdict;
}
return verdict;
}
bool compareDigitsInLine (int arr[][9], int rowSize, int columnSize,int startAtRow, int startAtColumn)
{
bool isDifferent = true;
for(int i=0; i < rowSize; i++)
{
for(int j=0; j < columnSize-1; j++)
{
for (int k = 1; k < columnSize - j; k++)
{
int rowIndex = startAtRow+i;
int colIndex = startAtColumn+j;
if ( arr[rowIndex][colIndex]==arr[rowIndex][colIndex+k] || arr[rowIndex ][colIndex] < 1 || arr[rowIndex ][colIndex] > 9 )
{
isDifferent = false;
return isDifferent;
}
}
}
}
return isDifferent;
}
bool compareDigitsInSmallSquares(int arr[][9], int rowSize, int columnSize,int startAtRow, int startAtColumn)
{
bool isDifferent = true;
int multiplier = rowSize/3;
int jumpRow =0;
int jumpCol = 0;
int colStartPos = 0;
int rowStartPos = 0;
int rowIndx = 0;
int colIndx = 0;
int rowRootIndx =0;
int colRootIndx =0;
int k=0, m=0;
while (jumpCol < multiplier)
{
while (jumpRow < multiplier )
{
for (int i = 0; i<rowSize/multiplier; i++ )
{
for (int j = 0; j <columnSize/multiplier; j++)
{
for ( k = 0; k < rowSize/multiplier - rowStartPos; k++)
{
if (i==0 && j==0 && k==0)
{
rowStartPos = 0;
colStartPos = 1;
}
if (i==0 && j ==0 && k>0)
{
rowStartPos = 0;
colStartPos = 0;
}
if (i==0 && j==1 && k==0)
{
rowStartPos = 0;
colStartPos = 2;
}
if (i==0 && j==1 && k>0)
{
rowStartPos = 0;
colStartPos = 0;
}
if (i==0 && j==2 && k==0)
{
rowStartPos = 0;
colStartPos = 3;
}
if (i==0 && j==2 && k>0)
{
rowStartPos = 0;
colStartPos = 0;
}
if (i==1 && j==0 && k==0)
{
rowStartPos = 1;
colStartPos = 1;
}
if (i==1 && j==0 && k>0)
{
rowStartPos = 1;
colStartPos = 0;
}
if (i==1 && j==1 && k==0)
{
rowStartPos = 1;
colStartPos = 2;
}
if (i==1 && j==1 && k>0)
{
rowStartPos = 1;
colStartPos = 0;
}
if (i==1 && j==2 && k==0)
{
rowStartPos = 1;//bez znaczenia
colStartPos = 3;
}
if (i==1 && j==2 && k>0)
{
rowStartPos = 1;
colStartPos = 0;
}
if (i==2 && j==0 && k==0)
{
rowStartPos = 2;
colStartPos = 1;
}
if (i==2 && j==1 && k==0)
{
rowStartPos = 2;
colStartPos = 2;
}
if (i==2 && j==2 && k==0)
{
rowStartPos = 2;
colStartPos = 3;
}
for ( m = 0; m < columnSize/multiplier - colStartPos; m++)
{
rowIndx = k + rowStartPos + jumpRow*multiplier;
colIndx = m + colStartPos + jumpCol*multiplier;
rowRootIndx = i + jumpRow*multiplier;
colRootIndx = j + jumpCol*multiplier;
if ( arr[rowRootIndx][colRootIndx]==arr[rowIndx][colIndx] || arr[rowRootIndx][colRootIndx] < 1 || arr[rowRootIndx][colRootIndx] > 9 )
{
isDifferent = false;
return isDifferent;
}
}
}
}
}
jumpRow ++;
}
jumpCol++;
if ( jumpRow == multiplier )
jumpRow=0;
}
return isDifferent;
}
void checkSudoku (int arr[][9], int rowSize, int columnSize,int initialPositionForRow, int initialPositionForColumn, int totalSumLimit)
{
bool isDifferent = true;
bool isCorrect = true;
bool isDifferentInSmallSquareCorrect = true;
isDifferentInSmallSquareCorrect = compareDigitsInSmallSquares(arr, rowSize, columnSize,initialPositionForRow,initialPositionForColumn);
isDifferent = compareDigitsInLine(arr, rowSize, columnSize,initialPositionForRow,initialPositionForColumn);
isCorrect = checkRowColumnSum(arr,rowSize,columnSize, totalSumLimit);
if (isDifferent == true && isCorrect == true && isDifferentInSmallSquareCorrect == true )
{
cout<<"TAK"<<endl;
}
else
{
cout<<"NIE"<<endl;
}
}
int main()
{
int rowSize = 9;
int columnSize = 9;
int totalSum = 45; // Suma w linii od 1 do 9 wynosi 45 - kryterium prawdy ulozenia liczb w Sudoku
int sudokuArray[9][9];
int j =0;
int numberOfTests;
string line;
cin >> numberOfTests;
getchar();
for (int i = 0; i < numberOfTests; i++)
{
while ( j<rowSize)
{
getline(cin, line);
convertStringToInt(line, sudokuArray, rowSize, j);
j++;
}
j=0;
checkSudoku(sudokuArray, rowSize, columnSize,0,0,totalSum);
}
return 0;
}