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
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//typedef tree < int , null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> TREE;
#define ll long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define pss pair<short,short>
#define pld pair<long double,long double >
#define ld long double
#define piii  pair<pii,int>
#define vii vector<pair<int,int> >
#define st first
#define nd second
#define speed ios::sync_with_stdio(false);cin.tie();cout.tie();
//#define int long long
//const int mod=1000000007;
//const int mod=1000003;
const int mod=998244353;
const int inf=1000000009;
const long long INF=1000000000000000009;
const long long big=1000000000000000;
const long double eps=0.0000000001;
using namespace std;
pii A[100005],B[100005];
int main()
{
    speed
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        int n;
        cin>>n;
        ll suma1=0,suma2=0;
        for(int j=1;j<=n;j++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            suma1+=(ll)a*b;
            suma2+=(ll)a*c;
			A[j]=mp(b,a);	
			B[j]=mp(c,a);
        }
        sort(A+1,A+n+1);
        sort(B+1,B+n+1);
        bool czy=true;
        int wsk=1;
        ll x=0,y=0,l1=0,l2=0,u=0;
        for(int j=1;j<=n;j++)
        {
        	x+=(ll)B[j].st*B[j].nd;
        	l1+=B[j].nd;
        	while(l2<l1)
        	{
        		ll d;
        		if(A[wsk].nd-u<=l1-l2)
        		{
        			d=A[wsk].nd-u;
        			u=0;
        			y+=(ll)A[wsk].st*d;
        			wsk++;
        		}
        		else
        		{
        			d=l1-l2;
        			u+=d;
        			y+=(ll)A[wsk].st*d;
        		}
        		
        		l2+=d;
        	}
        	//cout<<j<<" "<<x<<" "<<y<<endl;
        	if(x<y)
        	{
        		czy=false;
        		break;
        	}
        }
        wsk=n;
        x=0,y=0,l1=0,l2=0,u=0;
        for(int j=n;j>=1;j--)
        {
        	x+=(ll)B[j].st*B[j].nd;
        	l1+=B[j].nd;
        	while(l2<l1)
        	{
        		ll d;
        		if(A[wsk].nd-u<=l1-l2)
        		{
        			d=A[wsk].nd-u;
        			u=0;
        			y+=(ll)A[wsk].st*d;
        			wsk--;
        		}
        		else
        		{
        			d=l1-l2;
        			u+=d;
        			y+=(ll)A[wsk].st*d;
        		}

        		l2+=d;
        	}
        	if(x>y)
        	{
        		czy=false;
        		break;
        	}
        }
        if(suma1==suma2&&czy)
            cout<<"TAK\n";
        else
            cout<<"NIE\n";
    }
    return 0;
}