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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef long double ld;
typedef unordered_map<int, int> umii;
typedef vector<pair<ll,ll>> vpll;
typedef tuple<int,int,int> tp;
const ll MOD = 1e9+696969;

bool comp(vll& a, int l, int r)
{
    for(int i=l; i<=r; ++i)
    {
        if(a[i]==0)
        return false;
    }
    return true;
}

bool ok(vll& a, int l, int r)
{
    ll m=0;
    for(int i=l; i<=r; ++i)
    m+=a[i];

    int k=r-l+1;

    if(k==3)
    {
        ll x=a[l];
        ll y=a[l+1];
        ll z=a[l+2];
        if(x==1 and y== 0 and z==1)return false;
        if(x==0 and y==1 and z==0) return true;
    }

    if(k==5)
    {
        if(a[l]==1 and a[l+1]==3 and a[l+2]==2 and a[l+3]==2 and a[l+4]==3) return false;
    }

    return true;
}
void solve()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >>n;
        
        vll a(n+1);
        for(int i=1; i<=n; ++i) cin >> a[i];

        int l=-1, r=-1;

        for(int i=1; i<=n; ++i)
        {
            if(a[i]>0)
            {
                if(l<0)
                l=i;
                r=i;
            }
        }

        bool cmp = true;
        for(int i=l; i<=r; ++i)
        {
            if(a[i]==0)
            {
                cmp=false;
                break;
            }
        }

        if(!cmp)
        {
            cout << "NIE\n";
            continue;
        }

        int k=r-l+1;

        if(k==1)
        {
            if(a[l]==1)
            cout << "TAK\n";
            else
            cout << "NIE\n";
            continue;
        }

        if(k==2)
        {
            ll x=a[l];
            ll y=a[l+1];
            if(llabs(x-y)<=1) cout << "TAK\n";
            else
            cout << "NIE\n";
            continue;
        }

        if(ok(a,l,r))
        cout << "TAK\n";
        else
        cout << "NIE\n";

    }
}

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

    solve();
    return 0;
}