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
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define ld long double
#define pa pair<int,int>
#define st first
#define nd second
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;

const int MAX=100005;
const ll inf=1e18+9;
ll l[MAX],a[MAX],b[MAX];
ll sum1=0,sum2=0,akt1=0,akt2=0;
pair<ll,ll>tab1[MAX],tab2[MAX],tab3[MAX],tab4[MAX];

bool minimum(int n)
{
     int wskaznik=1;
      for (int i=1;i<=n;i++)
      {
         akt1+=tab3[i].nd;
         sum1+=tab3[i].st*tab3[i].nd;
         while (wskaznik<=n)
         {
           //cout<<"TERAZ "<<akt1<<" "<<akt2<<"\n";
           if (akt1-akt2<=tab4[wskaznik].nd)
           {
             tab4[wskaznik].nd-=(akt1-akt2);
             sum2+=(akt1-akt2)*tab4[wskaznik].st;
             akt2+=akt1-akt2;
             break;
           }
           else
           {
             sum2+=tab4[wskaznik].st*tab4[wskaznik].nd;
             akt2+=tab4[wskaznik].nd;
             wskaznik++;
           }
         }
         //cout<<"NOW "<<sum1<<" "<<sum2<<"\n";
         if (sum2<sum1)
         {
           return false;
         }
      }
      return true;
}

bool maximum(int n)
{
      int wskaznik=n;
      for (int i=n;i>=1;i--)
      {
         akt1+=tab1[i].nd;
         sum1+=tab1[i].st*tab1[i].nd;
         while (wskaznik>=1)
         {
           //cout<<"TERAZ "<<akt1<<" "<<akt2<<"\n";
           if (akt1-akt2<=tab2[wskaznik].nd)
           {
             tab2[wskaznik].nd-=(akt1-akt2);
             sum2+=(akt1-akt2)*tab2[wskaznik].st;
             akt2+=akt1-akt2;
             break;
           }
           else
           {
             sum2+=tab2[wskaznik].st*tab2[wskaznik].nd;
             akt2+=tab2[wskaznik].nd;
             wskaznik--;
           }
         }
        // cout<<"NOW "<<sum1<<" "<<sum2<<"\n";
         if (sum2>sum1)
         {
           return false;
         }
      }
      return true;
}

int main()
{
    BOOST;
    int t;
    cin>>t;

    for (int z=0;z<t;z++)
    {
      bool t=true;
      int n;
      cin>>n;
      sum1=0,sum2=0;
      for (int i=1;i<=n;i++)
      {
        cin>>l[i]>>a[i]>>b[i];
        sum1+=l[i]*a[i];
        sum2+=l[i]*b[i];
        tab1[i]=mp(a[i],l[i]);
        tab2[i]=mp(b[i],l[i]);
        tab3[i]=mp(a[i],l[i]);
        tab4[i]=mp(b[i],l[i]);
      }
      if (sum1!=sum2)
      {
        t=false;
      }
      sum1=0,sum2=0,akt1=0,akt2=0;
      sort(tab1+1,tab1+n+1);
      sort(tab2+1,tab2+n+1);
      sort(tab3+1,tab3+n+1);
      sort(tab4+1,tab4+n+1);

      if (t)t=minimum(n);
      akt1=0,akt2=0,sum1=0,sum2=0;
      if (t)t=maximum(n);
      if (t)cout<<"TAK\n";
      else cout<<"NIE\n";

    }

    return 0;
}