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

set<pair<int,int>> poprz;
set<pair<int,int>> curr;

const int MAXN = 1e6 + 5;
int tab[MAXN];

void solve(){
    int n;
    cin >> n;
    int c = 0;
    for(int i = 1; i <= n; ++i){
        cin >> tab[i];
        c += (tab[i] == 0);
    }
    if(c == n){
        cout << "NIE\n";
        return;
    }

    int pocz = -1;
    int kon = -1;
    for(int i = 1; i <= n; ++i){
        if(tab[i] != 0){
            pocz = i;
            break;
        }
    }

    for(int i = n; i > 0; --i){
        if(tab[i] != 0){
            kon = i;
            break;
        }
    }

    for(int i = pocz; i <= kon; ++i){
        if(tab[i] == 0){
            cout << "NIE\n";
            for(int j = 1; j <= n; ++j){
                tab[j] = 0;
            }
            return;
        }
    }


    poprz = {{-1, 0}};
    for(int i = pocz; i <= kon; ++i){
        for(auto u : poprz){
            if(i != pocz and u.first < 0) continue;
            if(u.second == 0){
                curr.insert({tab[i] - u.first - 2, 0});
                curr.insert({tab[i] - u.first - 2, 1});
                curr.insert({tab[i] - u.first - 2, 2});
            }
            else if(u.second == 1){
                curr.insert({tab[i] - u.first - 1, 1});
                curr.insert({tab[i] - u.first - 1, 2});
            }
            else{
                if(u.first == 0) continue;
                curr.insert({tab[i] - u.first, 2});
            }
        }
        poprz = curr;
        curr.clear();
        // cout << i << " i\n";
        // for(auto u : poprz){
        //     cout << u.first << " " << u.second << " u\n";
        // }
    }

    // or poprz.find(make_pair(0, 0)) != poprz.end() or poprz.find(make_pair(0, 1)) != poprz.end()
    if(poprz.find(make_pair(0, 2)) != poprz.end()){
        cout << "TAK\n";
    }
    else{
        cout << "NIE\n";
    }

    for(int i = 1; i <= n; ++i){
        tab[i] = 0;
    }
    poprz.clear();
    curr.clear();
    return;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}