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

#ifdef DEBUG
auto operator<<(auto&o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second <<')';}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x);
#else
#define LOG(x...)(void)0
#endif

typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

#define all(x)  (x).begin(),(x).end()
#define endl    '\n'
#define size(x)  x.size()

const ll INF = 9223372036854775806;
const ll MAX_N = 1e9+1;
const ll MOD = 1e9+7; // 998244353

void solveTestCase() {
    int N;
    cin >> N;
    vector<int> valuesArray(N);
    
    for (int &x : valuesArray) { cin >> x; }

    int L = 0;
    while (L < N && valuesArray[L] == 0) { L++; }
    int R = N - 1;
    while (R >= 0 && valuesArray[R] == 0) { R--; }
    if (L > R) { cout << "NIE" << endl; return; }

    bool isValid = true;
    for (int i = L; i <= R; ++i) {
        if (valuesArray[i] == 0) {
            isValid = false;
            break;
        }
    }
    if (!isValid) {
        cout << "NIE" << endl;
        return;
    }

    long long evenSum = 0, oddSum = 0;
    for (int i = 0; i < N; i++) {
        if ((i + 1) % 2 == 0) {
            evenSum += valuesArray[i];
        } else {
            oddSum += valuesArray[i];
        }
    }

    if (abs(evenSum - oddSum) > 1) { cout << "NIE" << endl; return; }
    if (valuesArray[L] < 1 || valuesArray[R] < 1) { cout << "NIE" << endl; return; }

    int maxValue = *max_element(valuesArray.begin() + L, valuesArray.begin() + R + 1);
    int rightPeak = R;
    while (rightPeak >= L && valuesArray[rightPeak] != maxValue) {
        rightPeak--;
    }

    for (int i = L; i < rightPeak; ++i) {
        if (valuesArray[i] > valuesArray[i + 1]) {
            isValid = false;
            break;
        }
    }
    if (!isValid) { cout << "NIE" << endl; return; }

    for (int i = rightPeak; i < R; ++i) {
        if (valuesArray[i] < valuesArray[i + 1]) {
            isValid = false;
            break;
        }
    }

    cout << (isValid ? "TAK" : "NIE") << endl;
}

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

    int T;
    cin >> T;

    while(T--) {
        solveTestCase();
    }
    return 0;
}