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

using namespace std;

int t, n;
map<long long, long long>aa;
map<long long, long long>bb;

bool check()
{
    long long s1=0, s2=0;
    vector<pair<long long, long long>>aaa;
    vector<pair<long long, long long>>bbb;
    for(auto e:aa)
        bbb.push_back({e.first, e.second});
    for(auto e:bb)
        aaa.push_back({e.first, e.second});
    int j=0, k=0, i=0;
    for(auto e:aaa)
    {
        s1+=e.first*e.second;
        i=e.second;
        while(i)
        {
            if(bbb[j].second>=i+k)
            {
                k+=i;
                s2+=bbb[j].first*i;
                i=0;
            }
            else //zuzyj reszte z tego i przejdz do nastepnego
            {
                s2+=(bbb[j].second-k)*bbb[j].first;
                i-=bbb[j].second-k;
                k=0;
                ++j;
            }
        }
        if(s1<s2)
            return 0;
    }
    return 1;
}

int main()
{
    long long a, b, c, w1, w2, mn, mx, mn1, mx1;
    scanf("%d", &t);
    while(t--)
    {
        mn=1e9+7; mx=0; mn1=1e9+6; mx1=1, w1=0, w2=0;
        scanf("%d", &n);
        for(int i=0;i^n;++i)
        {
            scanf("%lld%lld%lld", &a, &b, &c);
            aa[b]+=a;
            bb[c]+=a;
            w1+=a*b;
            w2+=a*c;
            mn=min(mn, b);
            mx=max(mx, b);
            mn1=min(mn1, c);
            mx1=max(mx1, c);
        }
        if(mx1<=mx && mn1>=mn && w1==w2 && check())
            printf("TAK\n");
        else
            printf("NIE\n");
        aa.clear();
        bb.clear();
    }
    return 0;
}