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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;

//#define DEBUG

#ifdef DEBUG
    #undef DEBUG
    #define DEBUG(x) x
#else
    #define DEBUG(x)
#endif

#define REP(x,n) for(int x=0;x<(n);++x)
#define VAR(x,n) __typeof(n) x = (n)
#define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x)
#define CONTAINS(x,elem) ((x).find(elem) != (x).end())

#define MAX_N 1000003

int input[MAX_N];

bool checkPreconditions(int* tab, int n) {
    REP(x,n)
        if (tab[x] == 0) {
            DEBUG(cerr<<"zero is present in the middle: " << x << " -> ";)
            return false;
        }
    
    int edges = 0;
    if (tab[0] > tab[1]) {
        if (tab[0]-tab[1]>1 || tab[2]>0) {
            DEBUG(cerr<<"leftmost " << 0 << " is too large (" << tab[1] << "<" << tab[0] << ") -> ";)
            return false;
        } else {
            ++edges;
            DEBUG(cerr << "found edge at " << 0 << endl;)
        }
    }

    if (tab[n-1] > tab[n-2]) {
        if (tab[n-1] - tab[n-2] > 1 || tab[n-3]>0) {
            DEBUG(cerr<<"rightmost " << n-1 << " is too large (" << tab[n-2] << "<" << tab[n-1] << ") -> ";)
            return false;
        } else {
            if (++edges == 2) {
                DEBUG(cerr << "found second edge at " << n-1 << " -> ";)
                return false;
            } else {
                DEBUG(cerr << "found edge at " << n-1 << endl;)
            }
        }
    }

    for(int x=1; x<n-1; ++x) {
        int diff = tab[x] - tab[x-1] - tab[x+1];
        if (diff >= 2) {
            DEBUG(cerr<<"single point " << x << " is too large -> ";)
            return false;
        } else if (diff == 1) {
            if (++edges == 2) {
                DEBUG(cerr << "found second edge at " << x << " -> ";)
                return false;
            }
            DEBUG(cerr << "found edge at " << x << endl;)
        }
    }
    return true;
}

bool inline isLeftMost(int x) {
    return x == 0;
}
bool inline isRightMost(int x, int n) {
    return x == n-1;
}

bool findRightSolution(int* tab, int n) {
    //going right, starting point is tab[0]
    DEBUG(
        cerr<<"entering solveRight with " << n << " arguments, total shift is " << tab-input << endl;
        REP(x,n)
            cerr << tab[x] << " ";
        cerr << endl;
    )
    if (n==1) {
        return tab[0]==1;
    }
    int remaining = tab[0]-1;
    REP(x,n-1) {
        int value = tab[x+1];
        if (remaining == value) {
            if (x+1 == n-1) {
                return true;
            } else {
                DEBUG(cerr<<"remaining==value ("<<remaining<<") for element "<<x+1<<" -> ";)
                return false;
            }
        } else if (remaining > value-1 && (remaining > value || x+1 > n-1)) {
            DEBUG(cerr<<" found another starting point at "<<x+1<<" when going right --> ";)
            return false;
        } else {
            remaining = value - remaining - 1;
        }
    }
    if (remaining >= 1) {
        DEBUG(cerr<<" remaining "<<remaining<<" at the end --> ";)
        return false;
    }
    return true;
}
bool findLeftSolution(int* tab, int n) {
//    DUPADEBUG(cout << "DUPA_left_";)
    return true;
    //going right, starting point is tab[0]
    DEBUG(
        cerr<<"entering solveLeft with " << n << " arguments, total shift is " << tab-input << endl;
        REP(x,n)
            cerr << tab[x] << " ";
        cerr << endl;
    )
    int remaining = tab[n-1]-1;
    for(int x=n-2; x>0; --x) {
        int value = tab[x];
        if (remaining == value) {
            DEBUG(cerr << "value = remaining at " << x << " - result is " << (tab[x-1]==0) << " --> ";)
            return tab[x-1] == 0;
        } else if (remaining > value) {
            DEBUG(cerr<<" found another starting point at "<<x<<" when going left --> ";)
            return false;
        } else {
            remaining = value - remaining;
        }
    }
    return true;
}

bool findCenterPointSolution(int* tab, int mid, int n) {
    int remaining = tab[0];
    REP(x,n-1) {
        if (x==mid)
            --remaining;
        int value = tab[x+1];
        if (remaining - value > 1) {
            DEBUG(cerr<<"too much remaining at " << x << " (" << remaining-value << ") -> ";)
            return false;
        } else if (remaining - value == 1) {
            DEBUG(cerr<<"found another center point at " << x << endl;)
            return false;
        } else if (remaining == value && x+1 < n-1) {
            // x+1 / x+2 is a separation for line
            DEBUG(cerr<<"found separation at " << x+1 << "/" << x+2 << " but not legal in center point solution --> ";)
            return false;
        } else {
            remaining = tab[x+1] - remaining;
        }
    }
    if (remaining > 0) {
        DEBUG(cerr<<" remaining "<<remaining<<" at the end --> ";)
        return false;
    }
    return true;
}

bool findSolution(int* tab, int n) {
    int remaining = tab[0];
    REP(x,n-1) {
        int value = tab[x+1];
        if (remaining - value > 1) {
            DEBUG(cerr<<"too much remaining at " << x << " (" << remaining-value << ") -> ";)
            return false;
        } else if (remaining - value == 1) {
            // x is a starting point for both directions
            DEBUG(cerr<<"found center point at " << x << endl;)
            return findCenterPointSolution(tab, x, n);
        } else if (remaining == value && x+1 < n-1) {
            // x+1 / x+2 is a separation for line
            DEBUG(cerr<<"found separation at " << x+1 << "/" << x+2 << endl;)
            return findRightSolution(tab+x+2, n-(x+2)) && findLeftSolution(tab, x+2);
        } else {
            remaining = tab[x+1] - remaining;
        }
    }
    if (remaining > 1) {
        DEBUG(cerr<<" remaining "<<remaining<<" at the end --> ";)
        return false;
    }
    return true;
}

//tab[0..n) is the real range with non-zero numbers
bool solve(int* tab, int n) {
    DEBUG(
        cerr<<"entering solve with " << n << " arguments, shift is " << tab-input << endl;
        REP(x,n)
            cerr << tab[x] << " ";
        cerr << endl;
    )
    if (n == 1) {
        DEBUG(cerr << "only non-zero element is " << tab[0] << " -> ";)
        return tab[0] == 1; // for 1-element array result is true only when exactly 1
    }

    return checkPreconditions(tab, n)
        && findSolution(tab, n);
}

bool solve() {
    int n;
    cin>>n;
    int firstNonZeroIndex=-1, lastNonZeroIndex=-1;
    REP(x, n) {
        cin>>input[x+1];
        if (input[x+1] != 0) {
            lastNonZeroIndex = x+1;
            if (firstNonZeroIndex == -1)
                firstNonZeroIndex = x+1;
        }
    }

    input[++n] = 0;
    //safety margins: tab[0] = tab[n+1] = 0
    if (!solve(input+firstNonZeroIndex, lastNonZeroIndex-firstNonZeroIndex+1)) {
        return false;
    }
    std::reverse(input+firstNonZeroIndex, input+lastNonZeroIndex+1);
    if (!solve(input+firstNonZeroIndex, lastNonZeroIndex-firstNonZeroIndex+1)) {
        return false;
    }
    // if (lastNonZeroIndex - firstNonZeroIndex > 2) {
    //     if (input[lastNonZeroIndex] == 1 && input[lastNonZeroIndex-1] == 1) {
    //         input[lastNonZeroIndex+1] = 1;
    //         input[lastNonZeroIndex+2] = 0;
    //         if (!solve(input+firstNonZeroIndex, lastNonZeroIndex-firstNonZeroIndex+2)) {
    //             return false;
    //         }
    //         std::reverse(input+firstNonZeroIndex, input+lastNonZeroIndex+2);
    //         if (!solve(input+firstNonZeroIndex, lastNonZeroIndex-firstNonZeroIndex+2)) {
    //             return false;
    //         }
    //         std::reverse(input+firstNonZeroIndex, input+lastNonZeroIndex+2);
    //         input[lastNonZeroIndex+1] = 0;
    //     }
    //     if (input[firstNonZeroIndex] == 1 && input[firstNonZeroIndex+1] == 1) {
    //         input[lastNonZeroIndex+1] = 1;
    //         input[lastNonZeroIndex+2] = 0;
    //         std::reverse(input+firstNonZeroIndex, input+lastNonZeroIndex+1);
    //         if (!solve(input+firstNonZeroIndex, lastNonZeroIndex-firstNonZeroIndex+2)) {
    //             return false;
    //         }
    //         std::reverse(input+firstNonZeroIndex, input+lastNonZeroIndex+2);
    //         if (!solve(input+firstNonZeroIndex, lastNonZeroIndex-firstNonZeroIndex+2)) {
    //             return false;
    //         }
    //     }
    // }
    return true;
}

int main() {
    ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    REP(x, t) {
        cout << (solve() ? "TAK" : "NIE") << endl;
    }
    return 0;
}