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

int t;
long long l[1000005], a[1000005], b[1000005], ail[1000005], bil[1000005];

void clean (int x, int y)
{
    for (int i = 0; i < x; i++)
        l[i] = a[i] = b[i] = 0;
    for (int i = 1; i <= y; i++)
        ail[i] = bil[i] = 0;
}

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

    cin >> t;
    for (int f = 0; f < t; f++)
    {
        int n, hi = 0;

        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> l[i] >> a[i] >> b[i];
            ail[a[i]] += l[i];
            bil[b[i]] += l[i];
            hi = max(max(hi, (int) a[i]), (int) b[i]);
        }

        bool koncz = false;
        for (int i = 1; i <= hi; i++)
        {
            if (bil[i] > ail[i])
            {
                cout << "NIE\n";
                koncz = true;
                clean(n, hi);
                break;
            }
            if (bil[i] < ail[i])
                break;
        }
        if (koncz)
            continue;
        for (int i = hi; i >= 1; i--)
        {
            if (bil[i] > ail[i])
            {
                cout << "NIE\n";
                koncz = true;
                clean(n, hi);
                break;
            }
            if (bil[i] < ail[i])
                break;
        }
        if (koncz)
            continue;

        long long srw = 0;
        for (int i = 0; i < n; i++)
            srw = srw + l[i] * (a[i] - b[i]);

        if (srw == 0)
            cout << "TAK\n";
        else
            cout << "NIE\n";

        clean(n, hi);
    }
}