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
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
#ifdef DEBUG
auto &operator << (auto &o, pair<auto, auto> p) {
	return o << "(" << p.first << ", " << p.second << ")";
}
auto operator << (auto &o, auto x) -> decltype(x.end(), o) {
	o << "{"; int i = 0;
	for (auto e : x) o << ","+!i++ << e;
	return o << "}";
}
#define debug(X...) cerr << "["#X"]: ", [](auto ...$) {((cerr << $ << "; "), ...) << endl;}(X)
#else
#define debug(...) {}
#endif

vector<int> trim(vector<int> a) {
	while (!a.empty() and a.back() == 0)
		a.pop_back();

	reverse(a.begin(), a.end());
	while (!a.empty() and a.back() == 0)
		a.pop_back();
	reverse(a.begin(), a.end());
	return a;
}


bool solve(vector<int> a) {
	int n = ssize(a);

	a = trim(a);
	debug(a);
	n = ssize(a);

	REP (i, n)
		if (a[i] == 0) {
			return false;
		}

	vector<int> give = a;
	FOR (i, 1, n - 1) 
		give[i] = a[i] - (give[i - 1] - 1);

	debug(give);

	bool has_zero = false;
	REP (i, n - 1) {
		if (give[i] == 0) {
			has_zero = true;
			break;
		}
	}

	if (has_zero || give.back() > 1 || give.back() < 0) {
		FOR (i, 1, n - 1) {
			if (i % 2 == 1)
				give[i]--;
			else
				give[i]++;
		}
	}

	debug(give);

	bool good = true;
	if (give.back() > a.back())
		good = false;
	
	REP (i, n - 1) {
		if (give[i] <= 0)
			good = false;
	}

	if (give.back() < 0 || give.back() > 1) {
		good = false;
	}

	return good;
}

void run_all_tests() {
	int test_count = int(pow(8, 8));
	int n = 8;

	int total_yes = 0;
	int total_xor = 0;

	auto gen_test = [](int t, int n) {
		vector<int> a(n);
		REP (i, n) {
			a.emplace_back((t % 8));
			t /= 8;
		}
		reverse(a.begin(), a.end());
		return a;
	};

	FOR (t, 1, test_count) {
		if (t % 1000000 == 0) 
			cerr << "test run: " << t << '\n';

		vector<int> a = gen_test(t, n);
		bool answer = solve(a);
		if (answer) {
			total_yes++;
			total_xor ^= t;
		}
	}

	cout << "YES count: " << total_yes << '\n';
	cout << "XOR: " << total_xor << '\n';
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin >> t;
	while (t--) {		
		int n; cin >> n;
		vector<int> a(n);
		REP (i, n) cin >> a[i];

		cout << (solve(a) ? "TAK\n" : "NIE\n");
	}
}