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
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define st first
#define nd second.first
#define rd second.second
#define For(i, l, r) for (int i = l; i <= r; i++)
#define Forcin(l, r, a)          \
    for (int i = l; i <= r; i++) \
        cin >> a[i];
#define Ford(i, l, r) for (int i = l; i >= r; i--)
#define ben(v) v.begin(), v.end()
#define LOCAL 0
#define LOCAL2 0
using namespace std;

const int M = 1000005, inf = 1e9;
int a[M], c = 0, searched = -1, ac[M];

void solve()
{
    c++;
    int n;
    cin >> n;
    For(i, 1, n)
            cin >>
        a[i];
    a[n + 1] = 0;
    /* if (c==100){
        cout << n<<'\n';
        For(i, 1, n)
            cout<<a[i]<<' ';
    } */
    int bigg = 0, b2 = 0;
    int fpos = inf, lpos = 0;
    For(i, 1, n)
    {
        if (a[i] == 0)
            continue;
        fpos = min(fpos, i);
        lpos = max(lpos, i);
        if (a[i] > a[i - 1] + a[i + 1])
        {
            bigg += (a[i] - a[i - 1] - a[i + 1]);
            if ((i > 2 && a[i - 2]) || (i < n - 1 && a[i + 2]))
                bigg += 2;
        }
        if (i % 2)
            b2 += a[i];
        else
            b2 -= a[i];
    }
    if (abs(b2) > 1)
        bigg += 2;
    For(i, fpos + 1, lpos - 1) if (a[i] == 0)
        bigg += 2;
    if (bigg > 1)
        cout << "NIE\n";
    else
        cout << "TAK\n";
}

void solve2()
{
    c++;
    bool cor = 1;
    int n;
    cin >> n;
    For(i, 1, n){
        cin >>a[i];
        ac[i]=a[i];    
    }
    if (c == searched)
    {
        cout << n << '\n';
        For(i, 1, n)
                cout
            << a[i] << ' ';
    }
    a[n + 1] = 0;
    int start = 0, end = 0, par = 0;
    For(i, 1, n) if (a[i] > 0)
    {
        start = i;
        break;
    }
    Ford(i, n, 1) if (a[i] > 0)
    {
        end = i;
        break;
    }
    for (int i = start; i <= end; i += 2)
        par += a[i];
    for (int i = start + 1; i <= end; i += 2)
        par -= a[i];
    For(i, start, end) if (a[i] == 0)
        cor = 0;
    if (abs(par) > 1)
        cor = 0;
    int used_ones = 0, ended = start - 1;
    if (par == -1)
        ended = start;
    For(i, start, end)
    {
        int mini = min(a[i], a[i + 1]);
        a[i] -= mini;
        a[i + 1] -= mini;
        if (mini > 0 && ended == i - 1)
            ended = i + 1;
        /* if (a[i] > 1 || (a[i] == 1 && (((i - start) % 2 == 0 && par != 1) || ((i - start) % 2 == 1 && par != -1))))
        {
            cor = 0;
        } */
        // used_ones += a[i];
        if (ended < i)
        {
            break;
        }
    }
    if (a[ended + 1] > 0)
        a[ended + 1]--;
    else if (a[ended - 1] > 0)
        a[ended - 1]--;
    For(i, start, end)
        used_ones += a[i];
    if (used_ones > 0 && cor && par != 0)
    {
        cor = 0;
    }
    else if (used_ones > 0 && cor)
    {
        used_ones = 0, ended = start;
        For(i, start, end)
        {
            int mini = min(ac[i], ac[i + 1]);
            ac[i] -= mini;
            ac[i + 1] -= mini;
            if (mini > 0 && ended == i - 1)
                ended = i + 1;
            if (ended < i)
            {
                break;
            }
        }
        if (ac[ended + 1] > 0)
            ac[ended + 1]--;
        else if (ac[ended - 1] > 0)
            ac[ended - 1]--;
        For(i, start, end)
            used_ones += ac[i];
        if (used_ones>0){
            cor=0;
        }
    }
    if (searched == -1 || c == searched)
        cout << (cor ? "TAK" : "NIE") << '\n';
}

signed main()
{
    cin.tie(0)->sync_with_stdio();
    if (LOCAL)
        freopen("wyl/all.in", "r", stdin);
    if (LOCAL2)
        freopen("local_out.txt", "w", stdout);
    int t;
    cin >> t;
    while (t--)
        solve2();
}