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
#include <bits/stdc++.h>
#define lld long long int
#define ff first
#define ss second
#define pii pair<lld,lld>
#define mp make_pair
using namespace std;
lld N,iloz,a,b,c,m;
pii p[1000010],k[1000010];

int main()
{
	scanf("%lld",&iloz);
	for(lld oo = 0;oo<iloz;++oo)
	{
		scanf("%lld",&N);
		for(lld i = 0;i<N;++i)
		{
			scanf("%lld%lld%lld",&a,&b,&c);
			p[i]=mp(b,a);
			k[i]=mp(c,a);
		}
		sort(p,p+N);
		sort(k,k+N);
		lld sum=0;
		lld indp=N-1,indk=N-1;
		bool fl=1;
		while(indp>=0 and indk>=0)
		{
			lld pom = min(p[indp].ss,k[indk].ss);
			sum+=p[indp].ff*pom-k[indk].ff*pom;
			if(sum<0)
			{
				break;
			}
			p[indp]=mp(p[indp].ff,p[indp].ss-pom);
			k[indk]=mp(k[indk].ff,k[indk].ss-pom);
			if(p[indp].ss==0)--indp;
			if(k[indk].ss==0)--indk;
		}
		if(indp!=-1 or indk!=-1 or sum!=0)fl=0;
		if(!fl)puts("NIE");
		else puts("TAK");
	}
}