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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<bits/stdc++.h>

#define pn std::cout << "NIE\n";

using namespace std;

void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

void solve(){
		int n;
		cin >> n;
		
		vector<int> a(n);
		for(int i = 0;i < n;i++){
			cin >> a[i];
		}
		
		if(n == 1){
			cout << (a[0] == 1 ? "TAK\n" : "NIE\n");
			return ;
		}
		
		if(a[0] > a[1] + 1){
			pn;
			return ;
		}
		if(a[n - 1] > a[n - 2] + 1){
			pn;
			return ;
		}
		for(int i = 1;i < n - 1;i++){
			if(a[i] > a[i - 1] + a[i + 1] + 1){
				pn;
				return ;
			}
		}
		
		bool ok = false;
		
		auto Do = [&]()->void{
			vector<int> b = a;
			auto rec = [&](auto &&rec, int pos)->void{
				b[pos] -= 1;
				if(pos == n - 1) return ;
				if(b[pos] > b[pos + 1]) return ;
				if(b[pos + 1] == b[pos]){
					b[pos] = b[pos + 1] = 0;
					return ;
				}
				b[pos + 1] -= b[pos];
				b[pos] = 0;
				rec(rec, pos + 1);
			};
			rec(rec, 0);
			for(int x : b) if(x != 0) return ;
			ok = true;
		};
		
		Do();
		reverse(a.begin(), a.end());
		Do();
		reverse(a.begin(), a.end());
		
		{ // A bit more complicated on where do you use it.
			
			int i = 0;
			while(a[i] == 0) i++;
			
			vector<int> wsio_zero(n + 1);
			wsio_zero[n] = 1;
			for(int i = n - 1;i >= 0;i--){
				wsio_zero[i] = wsio_zero[i + 1] & (a[i] == 0);
			}
			vector<int> b = a;
			
			map<int, int> pref_zero;
			pref_zero[-1] = 1;
			for(int i = 0;i < n;i++){
				pref_zero[i] = pref_zero[i - 1] & (a[i] == 0);
			}
			
			auto rec = [&](auto &&rec, int pos, int used_extra){ // also helps determine where came from l, r
				if(b[pos] == 0 || a[pos] < 0) return ;
				if(pos == n - 1 || wsio_zero[pos + 1]){
					//~ debug(pos, a[pos], used_extra);
					if(a[pos] == 0) ok = true;
					if(a[pos] == 1 && !used_extra && pref_zero[pos - 2]) ok = true;
					return ;
				}
				
				// Case #1:
				if(a[pos] > a[pos + 1] + 1) return ;
				// Case #2: 
				if(a[pos] == a[pos + 1] + 1){
					if(!used_extra) ok |= wsio_zero[pos + 2];
				}
				
				// Case #3:
				if(!used_extra){
					// 1) use 
					if(a[pos] > 0){
						a[pos + 1] -= (a[pos]);
						rec(rec, pos + 1, 1);
						a[pos + 1] += (a[pos]);
					}
					
					// 2) don't use
					a[pos + 1] -= (a[pos] + 1);
					rec(rec, pos + 1, 0);
					a[pos + 1] += (a[pos] + 1);
				} else{
					if(a[pos] == a[pos + 1]){
						ok |= wsio_zero[pos + 2];
						return ;
					}
					a[pos + 1] -= (a[pos] + 1);
					rec(rec, pos + 1, 1);
					a[pos + 1] += (a[pos] + 1);
				}
			};
			
			rec(rec, i, 0);
		}
		
		cout << (ok ? "TAK\n" : "NIE\n");
}

/*
5
1 3 2 2 3
mam TAK a ozcekuje NIE
 
2 3 mnie blokuje
  
*/
// 10077695
int main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);

	//~ freopen("all.in", "r", stdin);
	//~ freopen("all2.out", "w", stdout);
	
	int tt;
	cin >> tt;
	
	while(tt--){
		solve();
	}
	
	return 0;
}

/**
	CASES:
	4
  1 2 2 1
**/