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
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,LL> PIL;
typedef pair<LL,LL> PLL;
typedef vector<PIL> VPIL;
typedef vector<PLL> VPLL;
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
#define ALL(c) (c).begin(),(c).end()
#define FOREACH(it,c,i) for(auto it=(c).begin()+(i);it!=(c).end();++it)
#define SIZE(c) (int)(c).size()
int main()
{
    VPLL t,u,t2,u2;
    LL x,y,z;
    int d,i,n,j;
    scanf("%d",&d);
    while(d--){
        x=y=0;
        scanf("%d",&n);
        t.resize(n);
        u.resize(n);
        for(i=0;i<n;++i){
            scanf("%lld%lld%lld",&t[i].ND,&t[i].ST,&u[i].ST);
            u[i].ND=t[i].ND;
        }
        sort(ALL(t));
        sort(ALL(u));
//        for(auto p:t)
//            printf("%lld %lld\n",p.ST,p.ND);
//        for(auto p:u)
//            printf("%lld %lld\n",p.ST,p.ND);
        for(i=j=0;i<n && j<n;){
            z=min(t[i].ND,u[j].ND);
            x+=z*t[i].ST;
            y+=z*u[j].ST;
            t[i].ND-=z;
            if(t[i].ND==0)
                ++i;
            u[j].ND-=z;
            if(u[j].ND==0)
                ++j;
            if(x>y){
                printf("NIE\n");
                goto xyz;
            }
        }
        if(i!=j || x!=y)
            printf("NIE\n");
        else
            printf("TAK\n");
        xyz:;
    }
}