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
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

string tr(bool a) {
	return a ? "true": "false";
}

bool singleRound() {
	int n;
	cin >> n;
	
	bool isInRange = false;
	bool rangePassed = false;
	bool isDisconnected = false;
	bool justStarted = false;
	
	bool canBeNoEnd = true;
	bool canBeOneEndEven = false;
	bool canBeOneEndOdd = false;
	bool canBeBothEndsEven = false;
	bool canBeBothEndsOdd = false;
	bool canBeBothEndsEvenOdd = false;
	
	long long sum = 0;
	
	long long default_conns = 0;
	long long prev_def_conns = 0;

	for (int i = 0; i < n; i++) {
		long long num;
		cin >> num;
		
		if (sum <= 1) {
			sum += num;
		}
		
		if (isDisconnected) {
			continue;
		}
		
		if (rangePassed && num != 0) {
			isDisconnected = true;
		}
		
		if (num == 0 && isInRange) {
			rangePassed = true;
			isInRange = false;
		}
		
		if (num != 0) {
			if (!isInRange) {
				isInRange = true;
				justStarted = true;
			} else {
				justStarted = false;
			}
		}
		
		if (num == 0) {
			continue;
		}
		
		prev_def_conns = default_conns;
		default_conns = 2 * num - default_conns;

		swap(canBeOneEndOdd, canBeOneEndEven);
		swap(canBeBothEndsOdd, canBeBothEndsEven);

		if (canBeBothEndsEven) {
			if (prev_def_conns + 2 <= 0 || default_conns - 2 < 0) {
				canBeBothEndsEven = false;
			}
		}
		
		if (canBeBothEndsEvenOdd) {
			if (prev_def_conns <= 0 || default_conns < 0) {
				canBeBothEndsEvenOdd = false;
			}
		}
		
		if (canBeBothEndsOdd) {
			if (prev_def_conns - 2 <= 0 || default_conns + 2 < 0) {
				canBeBothEndsOdd = false;
			}
		}
		
		if (canBeOneEndOdd) {
			if (prev_def_conns - 1 <= 0 || default_conns + 1 <= 0) {
				canBeOneEndOdd = false;
			} else if (default_conns >= 0) {
				canBeBothEndsEvenOdd = true;
			}
		}
		
		if (canBeOneEndEven) {
			if (prev_def_conns + 1 <= 0 || default_conns - 1 <= 0) {
				canBeOneEndEven = false;
			} else if (default_conns - 2 >= 0) {
				canBeBothEndsEven = true;
			}
		}
		
		if (canBeNoEnd) {
			if (default_conns - 2 > 0) {
				canBeBothEndsEven = true;
			}
			
			if (default_conns - 1 > 0) {
				canBeOneEndEven = true;
			}
			
			if ((prev_def_conns <= 0 && !justStarted) || default_conns < 0) {
				canBeNoEnd = false;
			}
		}

	}
	
	if (isDisconnected) {
		return false;
	}
	
	if (canBeBothEndsEven && default_conns - 2 == 0) {
		return true;
	}
	
	if (canBeBothEndsOdd && default_conns + 2 == 0) {
		return true;
	}
	
	if (canBeBothEndsEvenOdd && default_conns == 0) {
		return true;
	}
	
	if (sum == 1) {
		return true;
	}
	
	return false;
}

int main() {
	int t;
	cin >> t;
	vector<bool> results(t);
	
	
	for (int i = 0; i < t; i++) {
		bool result = singleRound();
		results[i] = result;
	}
	
	for (int i = 0; i < t; i++) {
		string result = results[i] ? "TAK" : "NIE";
		cout << result << "\n";
	}
}