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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pii pair<int,int>
#define pl pair<ll,ll>
#define F first
#define S second
#define pq priority_queue
#define mkp make_pair
#define all(x) x.begin(), x.end()
#define deb(x) cout << #x << " = " << x << '\n';
#define deb2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << '\n';

constexpr int M = 1e9+7;
constexpr int N = 1e6+7;
constexpr bool debug = 0;
int n;
vector<int> nu;
vector<int> en;
pii dp[N];
int bobob;

string ans, ans2;

bool proc(int poc, int kon){
	if(poc > kon) return 0;				//BSO

	//deb2(poc,kon);
	if(poc == 0 && kon == 0){
		dp[0] = {nu[0]-1, nu[0]-1};
	}
	else if(poc == 0){
		dp[0] = {nu[0],nu[0]-1};
	}
	else dp[0] = {nu[0],nu[0]};

	//deb2(dp[0].F, dp[0].S);


	for(int i = 1; i < nu.size(); i++){
		if(dp[i-1].F <= 0) return 0;
		if(dp[i-1].S < 0) return 0;
		if(i-1 < poc && dp[i-1].S == 0) return 0;
		if(kon <= i-1 && dp[i-1].S == 0) return 0;

		if(poc == i && kon == i){
			dp[i] = {nu[i]-1-dp[i-1].F, nu[i]-1-dp[i-1].S};
		}
		else if(poc == i){
			dp[i] = {nu[i]-dp[i-1].F,nu[i]-1-dp[i-1].S};
		}
		else if(kon == i){
			dp[i] = {nu[i]-dp[i-1].F,nu[i]-1-dp[i-1].S};
		}
		else if(poc < i && i < kon){
			dp[i] = {nu[i]-dp[i-1].F+1,nu[i]-1-dp[i-1].S};
		}
		else{
			dp[i] = {nu[i]-dp[i-1].F,nu[i]-dp[i-1].S};
		}

		//deb2(dp[0].F, dp[0].S);
	}		//a_i, b_i

	return(dp[nu.size()-1] == mkp(0,0));
}

void solve(){
	cin >> n;
	en.resize(n);
	nu.clear();
	for(int i = 0; i < n; i++) cin >> en[i];
	int ob = 0;
	while(en[ob] == 0){
		ob++;
	}
	while(en[ob] != 0){
		nu.pb(en[ob]);
		ob++;
		if(ob >= n) break;
	}
	while(ob < n){
		if(en[ob] != 0){
			ans = "NIE";
			return;
		}
		ob++;
	}
	if(nu.size() == 1){			//edge case
		(nu[0] == 1) ? ans = "TAK" : ans = "NIE";
		return;
	}

	//for(auto k: nu) cout << k << ' ';
	//cout << '\n';

	//jedna z tych opcji dziala
	if(proc(0,nu.size()-1)){
		ans = "TAK";
	}
	else if(proc(1,nu.size()-1)){
		ans = "TAK";
	}
	else if(proc(0,nu.size()-2)){
		ans = "TAK";
	}
	else if(proc(1,nu.size()-2)){
		ans = "TAK";
	}
	else ans = "NIE";
	//cout << '\n';
}


int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int q; cin >> q;
	for(bobob = 0; bobob < q; bobob++){
		solve();
		cout << ans << '\n';
		/*
		cout << bobob << '\n';
		lin >> ans2;
		out << ans << '\n';
		if(ans != ans2){
			cout << "WRONG " << bobob << '\n';
			cout << n << '\n';
			for(auto k: en) cout << k << ' ';

			cout << '\n';
			deb2(ans, ans2);
			return 0;
		}
		*/
	}
	return 0;
}