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

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) {}
#endif

#define x first
#define y second
#define ir(a, x, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()

using ll = long long;
using pii = pair<int, int>;

pair<vec<int>, vec<int>> comp(vec<int> a) {
    int N = a.size();
    vec<int> l = {0}, r = {0};
    int x = 0;
    rep (n, 0, N) {
        x = a[n] - x;
        l.push_back(x);
    }
    reverse(all(a));
    x = 0;
    rep (n, 0, N) {
        x = a[n] - x;
        r.push_back(x);
    }
    reverse(all(a));
    reverse(all(r));
    return {l, r};
}

bool solve1(vec<int> a) {
    auto [l, r] = comp(a);
    debug(a);
    a = {};
    int N = a.size();
    debug(l);
    debug(r);
    vec<int> reqs;
    rep (i, 1, ((ll)l.size())-1) {
        if (abs(l[i] - r[i]) > 1) {
            return false;
        }
        if (l[i] < 0 || r[i] < 0) return false;
        if (l[i] == r[i] && (i != 0 && i != ((ll)l.size())-1)) {
            if (l[i] == 0) {
                reqs.push_back(3);
            } else {
                reqs.push_back(0);
            }
        } else if (l[i] > r[i]) {
            reqs.push_back(1);
        } else {
            reqs.push_back(2);
        }
    }
    // TODO: check r[0], l.back()
    // TODO: check if there are two contradicting requirements in the 1,2 case
    debug(reqs);

    // reqs.pop_back(); reverse(all(reqs)); reqs.pop_back(); reverse(all(reqs));
    if (reqs.size() == 0) {
        return true;
    }

    // if (r.back() != 0)
    //     if ((r[0] < 0) == (reqs[0] == 1)) return false;
    // if (l.back() != 0)
    //     if ((l.back() < 0) != (reqs.back() == 1)) return false;

    N = reqs.size();
    if (reqs[0] == 1 || reqs[0] == 2) {
        rep (n, 0, N) {
            debug(reqs[n]);
            if (reqs[n] != 1 && reqs[n] != 2) {
                return false;
            }
            if (n > 0 && reqs[n] == reqs[n-1]) {
                return false;
            }
        }
        return true;
    } else if (reqs[0] == 3 || reqs[0] == 0) {
        int i = -1;
        rep (n, 0, N) {
            if (reqs[n] != 3 && reqs[n] != 0) {
                return false;
            }
            if (reqs[n] == 3) {
                if (i == -1 || (i%2) == (n%2)) {
                    i = n;
                } else {
                    return false;
                }
            }
        }
        return true;
    } else {
        return false;
    }
}

bool solve() {
    int N; cin >> N;
    vec<int> a(N);
    rep (n, 0, N) cin >> a[n];
    int l = 0, r = N-1;
    while (a[l] == 0) l++;
    while (a[r] == 0) r--;
    r++;
    rep (n, l, r) {
        if (a[n] == 0) return false;
    }
    vec<int> t(a.begin() + l, a.begin() + r);
    if (t.size() == 1) {
        return t[0] == 1;
    }
    return solve1(t);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t; cin >> t;
    while (t--) {
        cout << (solve() ? "TAK" : "NIE") << "\n";
    }
    return 0;
}