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

const int N = 1e6 + 5;

ll A[N];
pair<ll, ll> DP[N][2];

void CountDP(int start, int end)
{
    DP[start][0] = {A[start] - 1, 0};
    for(int i = start + 1; i <= end; i++) {
        if(DP[i-1][0].second == 0) {
            if(DP[i-1][0].first < A[i] - 1)
                DP[i][0] = {A[i] - 2 - DP[i-1][0].first, 0};
            else if(DP[i-1][0].first == A[i] - 1)
                DP[i][0] = {0, 1};
            else
                DP[i][0] = {A[i] - 1, 2};
        } else if(DP[i-1][0].second == 1) {
            if(DP[i-1][0].first <= A[i] - 1)
                DP[i][0] = {A[i] - DP[i-1][0].first - 1, 1};
            else
                DP[i][0] = {A[i] - 1, 2};
        } else
            DP[i][0] = {A[i] - 1, 2};  
    }

    DP[end][1] = {A[end] - 1, 0};
    for(int i = end-1; i >= start; i--) {
        if(DP[i+1][1].second == 0) {
            if(DP[i+1][1].first < A[i] - 1)
                DP[i][1] = {A[i] - 2 - DP[i+1][1].first, 0};
            else if(DP[i+1][1].first == A[i] - 1)
                DP[i][1] = {0, 1};
            else
                DP[i][1] = {A[i] - 1, 2};
        } else if(DP[i+1][1].second == 1) {
            if(DP[i+1][1].first <= A[i] - 1)
                DP[i][1] = {A[i] - DP[i+1][1].first - 1, 1};
            else
                DP[i][1] = {A[i] - 1, 2};
        } else  
            DP[i][1] = {A[i] - 1, 2};
    }
}

bool Solve(int n)
{
    int start = 0, end = n-1;
    while(start < n && A[start] == 0)
        start++;
    while(end >= 0 && A[end] == 0)
        end--;

    for(int i = start; i <= end; i++) {
        if(A[i] == 0)
            return false;
    }
    if(start == n)
        return true;
    
    CountDP(start, end);
    
    if(DP[end][0].first == 0 && DP[end][0].second != 2)
        return true;
    if(DP[start][1].first == 0 && DP[start][1].second != 2)
        return true;

    for(int i = start + 1; i <= end - 1; i++) {
        if(DP[i][0].second == 0) {
            if(DP[i+1][1].second == 0 && (DP[i][0].first == DP[i+1][1].first || DP[i][0].first - 1 == DP[i+1][1].first))
                return true;
            else if(DP[i+1][1].second == 1 && DP[i+1][1].first == DP[i][0].first)
                return true;
        }

        if(DP[i][1].second == 0) {
            if(DP[i-1][0].second == 0 && (DP[i][1].first == DP[i-1][0].first || DP[i][1].first - 1 == DP[i-1][0].first))
                return true;
            else if(DP[i-1][0].second == 1 && DP[i-1][0].first == DP[i][1].first)
                return true;
        }
    }
    return false;
}

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

    int t, n;
    cin >> t;

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

        for(int i = 0; i < n; i++)
            cin >> A[i];
        
        if(Solve(n))
            cout << "TAK\n";
        else
            cout << "NIE\n";
    }
}