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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <algorithm>


using namespace std;


static const int STATE_BEFORE = 0;
static const int STATE_IN = 1;
static const int STATE_AFTER = 2;


static int t, n, iL, iR, iSize;

static int a[1'000'000];


static inline bool CanReductLeft()
{
    return a[iL] < a[iL + 1];
}


static inline bool CanReductRight()
{
    return a[iR] < a[iR - 1];
}


static inline void ReductLeft()
{
    a[iL + 1] -= a[iL];

    iL++;
    iSize--;
}


static inline void ReductRight()
{
    a[iR - 1] -= a[iR];

    iR--;
    iSize--;
}


static bool Reducto()
{
    iSize = iR - iL + 1;

    while (iSize > 1)
    {
        if (iSize == 2)
        {
            return abs(a[iL] - a[iR]) <= 1;
        }

        if (CanReductLeft())
        {
            ReductLeft();
        }
        else
            if (CanReductRight())
            {
                ReductRight();
            }
            else
                return false;
    }

    return a[iL] == 1;
}


static bool ReadData()
{
    cin >> n;

    int x;
    int iState = STATE_BEFORE;

    for (int i = 0; i < n; ++i)
    {
        cin >> x;

        a[i] = x;

        if (iState == STATE_BEFORE)
        {
            if (x != 0)
            {
                iL = i;
                iR = i;
                iState = STATE_IN;
            }
            continue;
        }

        if (iState == STATE_IN)
        {
            if (x == 0)
            {
                iState = STATE_AFTER;
            }
            else
            {
                iR = i;
            }
            continue;
        }

        // iState == STATE_AFTER

        if (x != 0)
            return false;
    }

    return true;
}


static void SolveCase()
{
    if (!ReadData())
    {
        cout << "NIE";
        return;
    }

    if (Reducto())
    {
        cout << "TAK";
    }
    else
    {
        cout << "NIE";
    }
}


int main()
{
    cin >> t;

    for (int i = 1; ; ++i)
    {
        SolveCase();

        if (i == t)
            break;

        cout << endl;
    }
}