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

const string TAK = "TAK";
const string NIE = "NIE";

void printVector(const vector<int> &vec) {
	for (int v : vec) {
		cout << v << " ";
	}
	cout << endl;
}
bool checkCountingExists(vector<int> slcs, bool fromLeft) {
//	printVector(slcs);
	const int S = slcs.size();
	if (S == 1) {
		return slcs[0] < 2;
	}
	if (S > 2) {
		if (slcs[0] > slcs[1] || slcs[S - 2] < slcs[S - 1]) {
			return false;
		}
	}

	int sub;

	if (fromLeft) {
		for (int i = 1; i < S; i++) {
			sub = min(slcs[i], slcs[i - 1]);
			slcs[i] -= sub;
			slcs[i - 1] -= sub;
			if (i < S - 1) {
				if (slcs[i] == 0 && slcs[i - 1] > 0) {
					return false;
				}
			}
		}
	} else { // from right
		for (int i = S - 1; i > 0; i--) {
			sub = min(slcs[i], slcs[i - 1]);
			slcs[i] -= sub;
			slcs[i - 1] -= sub;

			if (i > 1) {
				if (slcs[i - 1] == 0 && slcs[i] > 0) {
					return false;
				}
			}
		}
	}

//	printVector(slcs);

	int count1 = 0;
	bool greater = false;
	for (int i = 0; i < S; i++) {
		if (slcs[i] == 0) {
			continue;
		} else if (slcs[i] == 1) {
			count1++;
		} else {
			greater = true;
			break;
		}
	}

	if (greater || count1 > 1) {
		return false;
	} else if (count1 < 2) {
		return true;
	} else {
		return false;
	}
}

int main() {
//	ifstream cin("C:\\Users\\chodnik\\Desktop\\dx\\tests4\\r2b065086.in");
//	ifstream cin("tests/0a.in");
//	ifstream cin("tests/1034.in");
//	ifstream cin("tests/2071.in");
//	ifstream cin("tests2/38.in");

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	int t;
	cin >> t;
	for (int test = 0; test < t; test++) {
		int n, num;
		cin >> n;
		int i = 0;
		vector<int> selections;

		for (; i < n; i++) {
			cin >> num;
			if (num > 0) {
				selections.push_back(num);
				i++;
				break;
			}
		}
		for (; i < n; i++) {
			cin >> num;
			if (num > 0) {
				selections.push_back(num);
			} else {
				i++;
				break;
			}
		}
		bool consistent = true;
		for (; i < n; i++) {
			cin >> num;
			if (num > 0) {
				consistent = false;
			}
		}

		if (consistent) {
			bool yes = checkCountingExists(selections, false)
					&& checkCountingExists(selections, true);
			cout << (yes ? TAK : NIE) << endl;
//			cout << (checkCountingExists(selections, false) ? TAK : NIE)
//					<< endl;
		} else {
//			printVector(selections);
			cout << NIE << endl;
		}
	}

	return 0;
}