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
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif

#define pb push_back

typedef long long ll;
typedef long double ld;
const int maxN = 1e6 + 10;
ll a[maxN];
ll b[maxN];
int n;
ll c[maxN];
ll p[maxN];
void solve() {
    cin >> n;
    int l = -1;
    int r = -1;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] != 0 && l == -1) l = i;
        if (a[i] != 0) r = i;
    }
    for (int i = l; i <= r; i++) {
        if (a[i] == 0) {
            cout << "NIE\n";
            return;
        }
    }
    n = r - l + 1;
    for (int i = l; i <= r; i++) {
        b[i - l + 1] = a[i];
    }
    for (int x = 1; x <= 2; x++) {
        for (int y = n - 1; y <= n; y++) {
            if (x > y) continue;
            assert(1 <= x && x <= y && y <= n);
            for (int i = 1; i <= n; i++) {
                c[i] = b[i];
                if (x <= i && i <= y) c[i]--;
            }
            for (int i = 1; i <= n; i++) {
                p[i] = c[i] - p[i - 1];
            }
            if (p[n] != 0) continue;
            bool ok = true;
            for (int i = 1; i < n; i++) {
                if (p[i] < 0) ok = false;
            }
            if (ok) {
                cout << "TAK\n";
                return;
            }
        }
    }
    cout << "NIE\n";
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("all.in", "r", stdin);
#endif
    auto start = std::chrono::high_resolution_clock::now();
    int tst;
    cin >> tst;
    while (tst--) {
        solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    std::cerr << "Execution Time: "
              << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
              << " ms" << std::endl;
    return 0;
}