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
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define BOOST ios_base::sync_with_stdio(0), cin.tie(0)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

const int N = 1e6 + 5;
int toyc[N];

void solve(){
	int n; cin >> n;
	vector<int> t;
	for(int i=1; i<=n; i++){
		cin >> toyc[i];
	}
	for(int i=1; i<=n; i++){
		if(toyc[i]){
			if(!toyc[i-1] && t.size()){
				cout << "NIE\n";
				return;
			}
			t.pb(toyc[i]);
		}
	}
	n = t.size();

	if(n == 0){
		cout << "NIE\n";
		return;
	}
	if(n == 1){
		if(t[0] == 1){
			cout << "TAK\n";
		}
		else{
			cout << "NIE\n";
		}
		return;
	}

	auto go = [](int n, vector<int> t) -> bool {
		// for(int i=0; i<n; i++){
		// 	cout << t[i] << " ";
		// }
		// cout << "\n";
		for(int i=0; i<n-1; i++){
			if(t[i] < 0){
				return false;
			}
			if(t[i] > 0){
				int x = t[i];
				t[i] -= x;
				t[i+1] -= x;
			}
		}
		if(t[n-1] != 0) return false;
		return true;
	};
	
	for(int i=1; i<n-1; i++){
		t[i]--;
	}
	if(go(n, t)){
		cout << "TAK\n";
		return;
	}
	t[n-1]--;
	if(go(n, t)){
		cout << "TAK\n";
		return;
	}
	t[0]--;
	if(go(n, t)){
		cout << "TAK\n";
		return;
	}
	t[n-1]++;
	if(go(n, t)){
		cout << "TAK\n";
		return;
	}
	cout << "NIE\n";
	return;
}

int main(){
	BOOST;
	int q; cin >> q;
	while(q--) solve();
}