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
// Author: Olaf Surgut (surgutti)
// Created on 11-03-2025 17:56:44
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define ll long long
#define ld long double

#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(x) begin(x),end(x)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)

auto& operator<<(auto &o, pair<auto, auto> p) {
	return o << "(" << p.st << ", " << p.nd << ")";}
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
	o << "{"; int i=0; for (auto e : x) o << ","+!i++ << e;
	return o << "}";}

#ifdef LOCAL
#define debug(x...) cerr << "[" #x "]: ", [](auto...$) { \
	((cerr << $ << "; "),...) << endl; }(x)
#else
#define debug(...)
#endif

#define rep(i,a,b) for(int i = a; i < (b); i++)
using pii = pair<int, int>;
using vi = vector<int>;

void solve() {
	int n;
	cin >> n;
	vi a(n);

	rep(i, 0, n)
		cin >> a[i];
	
	while (a.back() == 0)
		a.pop_back();
	
	int s = 0;
	while (a[s] == 0)
		s++;	

	a.erase(begin(a), begin(a) + s);
	n = sz(a);

	if (find(all(a), 0) != end(a)) {
		cout << "NIE\n";
		return;
	}

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

	// ---
	bool ok = true;

	array<vi, 2> bad;
	rep(k, 0, n - 1) {
		a[k + 1] -= a[k];
	
		if (a[k] <= 0) {
			bad[k % 2].pb(k);
		}
	}

	if (sz(bad[0]) > 0 && sz(bad[1]) > 0) {
		cout << "NIE\n";
		return;
	}

	if (a[n - 1] == 0) {
		if (sz(bad[0])) {
			for (int i = 2; i < n - 2; i += 2)
				a[i]++;
		}
		else {
			for (int i = 1; i < n - 2; i += 2)
				a[i]++;
		}
	}
	else
	if (a[n - 1] == 1) {
		a[n - 1]--;
		for (int i = n - 2; i >= 0; i -= 2)
			a[i]++;
	}
	else
	if (a[n - 1] == -1) {
		for (int i = n - 1; i >= 0; i -= 2)
			a[i]++;
	}
	else {
		ok = false;
	}

	rep(i, 0, n - 1)
		if (a[i] <= 0)
			ok = false;

	if (ok) {
		cout << "TAK\n";
	}
	else {
		cout << "NIE\n";
	}
}

signed main() {
	cin.tie(0)->sync_with_stdio(0);

	int tt = 1;
	cin >> tt;
	while (tt--)
		solve();

	return 0;
}