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

using namespace std;

typedef long long ll;


bool g(int *arr, int start, int end) {
    int length = end - start + 1;
    if (length == 1) {
        return arr[start] == 1;
    }
    if (arr[start] == 0 || arr[end] == 0) {
        return false;
    }
    if (length == 2) {
        return arr[start] == arr[end];
    }

    if (arr[start] <= arr[start + 1]) {
        arr[start + 1] += 1 - arr[start];
        return g(arr, start + 1, end);
    }

    return false;
}

bool f(int *arr, int start, int end) {
    int length = end - start + 1;
    if (length == 1) {
        return (arr[start] == 0) | (arr[start] == 1);
    }
    if (length == 2) {
        return abs(arr[end] - arr[start]) <= 1;
    }

    if (arr[start] == 0) {
        return f(arr, start + 1, end);
    }
    if (arr[end] == 0) {
        return f(arr, start, end - 1);
    }

    if (arr[start] > arr[start + 1]) {
        return false;
    }
    if (arr[start] < arr[start + 1]) {
        arr[start + 1] -= arr[start];
        return f(arr, start + 1, end);
    }

    if (arr[end] > arr[end - 1]) {
        return false;
    }
    if (arr[end] < arr[end - 1]) {
        arr[end - 1] -= arr[end];
        return f(arr, start, end - 1);
    }

    if (length == 3) {
        return (arr[start] == 1) & (arr[start + 1] == 1) & (arr[end] == 1);
    }
    if (length == 4) {
        return true;
    }

    return g(arr, start + 2, end - 2);
}


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

    int t;
    cin >> t;

    bool res[t + 1];

    for (int i = 1; i <= t; i++) {
        int n;
        cin >> n;
        int arr[n + 1];
        for (int j = 1; j <= n; j++) {
            cin >> arr[j];
        }
        res[i] = f(arr, 1, n);
    }

    for (int i = 1; i <= t; i++) {
        if (res[i]) {
            cout << "TAK\n";
        }
        else {
            cout << "NIE\n";
        }
    }

    return 0;
}