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
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MAXN = 1e6+9;

ll n, t, tab[MAXN], lewo[MAXN], prawo[MAXN];

void solve() {
  ll start{0}, end{n+1};
  cin >> n;
  for(int i = 0; i <= n+1; ++i) tab[i] = lewo[i] = prawo[i] = 0;
  for(int i = 1; i <= n; ++i) {
    cin >> tab[i];
    if(tab[i] && !start) start = i;
    else if(!tab[i] && end == (n+1)) end = i-1;
    lewo[i] = tab[i];
    prawo[i] = tab[i];
    lewo[i] -= lewo[i-1];
  }

  for(int i = n; i >= 1; --i) {
    prawo[i] -= prawo[i+1];
  }
  

  
  for(int i = 1; i <= n; ++i) {
    if(!tab[i]) continue;
    if(tab[i] > tab[i-1] + tab[i+1] + 1) {
      cout << "NIE\n";
      return;
    }
    else if(tab[i] == tab[i-1] + tab[i+1] + 1) {
      tab[i] = tab[i-1] = tab[i+1] = 0;
      for(int j = 1; j <= n; ++j) {
        if(tab[j] != 0) {
          cout << "NIE\n";
          return;
        }
      }
      cout << "TAK\n";
      return;
    }
  }

  if(n >= 2 && tab[start] == tab[start+1]) {
    ll tt = tab[start]-1;
    for(int i = start+1; i <= n; ++i) {
      tt = tab[i] - tt;
    }
    if(!tt) {
      cout << "TAK\n";
      return;
    }
  }

  if(n >= 2 && tab[end] == tab[end-1]) {
    ll tt = tab[end]-1;
    for(int i = end-1; i >= 0; --i) {
      tt = tab[i] - tt;
    }
    if(!tt) {
      cout << "TAK\n";
      return;
    }
  }
  
  bool drop = 0;
  int en = 0;
  for(int i = start+1; i <=end; ++i) {
    if(!drop && tab[i] >= tab[i-1]) en = i;
    else drop = 1;
    
    if(drop && tab[i] > tab[i-1]) {
      cout << "NIE\n";
      return;
    }
  }
  if(tab[en] - lewo[en-1] - prawo[en+1] <= 1) cout << "TAK\n";
  else cout << "NIE\n";
}

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