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


using namespace std;

const int N = 1000003;
int aa[N], a[N];


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

    int t;
    cin >> t;

    while(t--) {
        int n;
        cin >> n;

        for(int i = 1; i <= n; i++) {
            cin >> aa[i];
        }
        int start = 1, end = n;
        while(start <= n && aa[start] == 0) start++;
        while(end >= 1 && aa[end] == 0) end--;

        for(int i = start; i <= end; i++) {
            a[i - start + 1] = aa[i];
        }
        n = end - start + 1;

        int right_edges = 0;
        bool done = false;
        int parity = -1;
        for(int i = 1; i < n; i++) {
            right_edges = 2 * a[i] - right_edges;
            if(right_edges < 0) {
                cout << "NIE\n";
                done = true;
                break;
            }
            if(right_edges == 0) {
                if(parity != -1  &&  parity != i % 2) {
                    cout << "NIE\n";
                    done = true;
                    break;
                }
                parity = i % 2;
            }
        }

        if(done) continue;

        if(parity == -1) {
            if(right_edges == 2*a[n]  ||  right_edges + 2 == 2*a[n]  ||  right_edges == 2*a[n] + 2) cout << "TAK\n";
            else cout << "NIE\n";
        }
        else {
            if(right_edges == 2*a[n]) {
                cout << "TAK\n";
            }
            else {
                if(parity == (n-1)%2  &&  right_edges + 2 == 2*a[n]) {
                    cout << "TAK\n";
                    continue;
                }
                
                if(parity != (n-1)%2  &&  right_edges == 2*a[n] + 2) {
                    cout << "TAK\n";
                    continue;
                }

                cout << "NIE\n";
            }
        }
    }

    return 0;
}