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
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
#include <assert.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
int T,n,l[SZ],a[SZ],b[SZ];
pii p[SZ],q[SZ];
map<ll,pair<ll,ll>> w;
void go(pii*t,bool f)
{
	ll cd=0;
	for(int i=1;i<=n;++i)
	{
		(f?w[cd+1].se:w[cd+1].fi)+=t[i].fi;
		cd+=t[i].se;
		(f?w[cd+1].se:w[cd+1].fi)-=t[i].fi;
	}
}
bool jud()
{
	w.clear();
	sort(p+1,p+1+n);
	sort(q+1,q+1+n);
	go(p,0); go(q,1);
	ll s0=0,s1=0;
	for(auto&pp:w)
	{
		s0+=pp.se.fi;
		s1+=pp.se.se;
		pp.se.fi=s0;
		pp.se.se=s1;
	}
	ll cs=0;
	for(auto uu=w.begin();uu!=w.end();++uu)
	{
		auto vv=uu; ++vv; if(vv==w.end()) break;
		cs+=(vv->fi-uu->fi)*(uu->se.fi-uu->se.se);
		if(cs>0) return 0;
	}
	return 1;
}
void sol()
{
	scanf("%d",&n);
	ll s=0;
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",l+i,a+i,b+i),
		p[i]=pii(a[i],l[i]),
		q[i]=pii(b[i],l[i]),
		s+=1LL*l[i]*(b[i]-a[i]);
	bool aa=(s==0)&&jud();
	if(aa) puts("TAK");
	else puts("NIE");
}
int main()
{
	scanf("%d",&T);
	while(T--) sol();
}