1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<bits/stdc++.h>

using namespace std;



void solve()
{
	int a;
	cin>>a;
	
	vector<int>licz;
	int c=0;
	
	vector<int>li;
	for(int z=0;z<a;z++)
	{
		int k;
		cin>>k;
		
		li.push_back(k);
	}
	
	for(int z=0;z<a;z++)
	{
		int k=li[z];
		
		if(k==0&&licz.size()==0)continue;
		
		if(k&&c)
		{
			cout<<"NIE\n";
			return;
		}
		if(k)licz.push_back(k);
		
		if(k==0)c=1;
	}
	
	while(licz.size())
	{
		if(licz.size()==1)
		{
			if(licz[0]==1)break;
			else
			{
				cout<<"NIE\n";
				return;
			}
		}
		
		int saj=licz.size()-1;
		if(licz[saj]<licz[saj-1])
		{
			licz[saj-1]-=licz[saj];
			licz.pop_back();
			continue;
		}
		if(licz[saj]>licz[saj-1]+1)
		{
			cout<<"NIE\n";
			return;
		}
		if(licz[saj]>licz[saj-1])
		{
			if(saj+1==2)cout<<"TAK\n";else cout<<"NIE\n";
			return;
		}
		while(licz.size())
		{
			int saj=licz.size()-1;
			if(saj+1==1)
			{
				if(licz[saj]==0||licz[saj]==1)
				{
					cout<<"TAK\n";
					return;
				}
				cout<<"NIE\n";
				return;
			}
			if(licz[saj]>licz[saj-1])
			{
				if(licz[saj]>licz[saj-1]+1)
				{
					cout<<"NIE\n";
					return;
				}
				if(saj+1==2)cout<<"TAK\n";else cout<<"NIE\n";
				return;
			}
			licz[saj-1]-=licz[saj]-1;
			licz.pop_back();
			
		}
		break;
	}
	
	cout<<"TAK\n";
}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int h=1;
	cin>>h;
	while(h--)solve();
	
	return 0;
}