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
#include<bits/stdc++.h>
using namespace std;
int t,n,A,B,C;
long long Q1,Q2,pmax,dmax,pmin,dmin;
vector<pair<int,int> >t1,t2;
bool cmp(const pair<int,int>&a, const pair<int,int>&b)
{
    return a.second<b.second;
}

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

cin>>t;
for(int tt=0;tt<t;tt++)
{
    cin>>n;
    Q1=0;
    Q2=0;
    pmax=0;
    pmin=0;
    dmax=0;
    dmin=0;
    for(int i=0;i<n;i++)
    {
        cin>>A>>B>>C;
        Q1+=A*B;
        Q2+=A*C;
        pair<int,int>par;
        par.first=A;
        par.second=B;
        t1.push_back(par);
        par.second=C;
        t2.push_back(par);
    }
    sort(t1.begin(),t1.end(),cmp);
    sort(t2.begin(),t2.end(),cmp);
    if(t2[n-1].second==t1[n-1].second)
    {
        for(int i=n-1;i>=0;i--)
        {
            if(t1[i].second!=t1[n-1].second && t2[i].second!=t2[n-1].second)
            {
                break;
            }
            if(t1[i].second==t1[n-1].second)
            {
                pmax+=t1[i].first;
            }
            if(t2[i].second==t2[n-1].second)
            {
                dmax+=t2[i].first;
            }
        }
        if(dmax>pmax)
        {
            cout << "NIE" << endl;
            t1.clear();
            t2.clear();
            continue;
        }
    }
    if(t2[0].second==t1[0].second)
    {
        for(int i=0;i<=n-1;i++)
        {
            if(t1[i].second!=t1[0].second && t2[i].second!=t2[0].second)
            {
                break;
            }
            if(t1[i].second==t1[0].second)
            {
                pmin+=t1[i].first;
            }
            if(t2[i].second==t2[0].second)
            {
                dmin+=t2[i].first;
            }
        }
        if(dmin>pmin)
        {
            cout << "NIE" << endl;
            t1.clear();
            t2.clear();
            continue;
        }
    }
    if(t2[n-1].second>t1[n-1].second || t2[0].second<t1[0].second)
    {
        cout << "NIE" << endl;
    }
    else
    {
        if(Q1==Q2)
        {
            cout << "TAK" << endl;
        }
        else
        {
            cout << "NIE" << endl;
        }
    }
    t1.clear();
    t2.clear();
}
}