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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <bits/stdc++.h>
//#define dbg(...) fprintf(stderr,__VA_ARGS__)
#define pb push_back
#define ff first
#define ss second
#define lcm(a,b) 

using namespace std;

struct frc {
	long long a,b;
};

frc operator+(const frc &a, const frc &b) {
	int l,m=a.b/__gcd(a.b,b.b)*b.b; frc w;
	l=(m/a.b)*a.a+(m/b.b)*b.a;
	if (l>0) {
		w.a=l/__gcd(l,m);
		w.b=m/__gcd(l,m);
	} else {
		w.a=l; w.b=m;
	}
	return w;
}

frc operator-(const frc &a, const frc &b) {
	int l,m=a.b/__gcd(a.b,b.b)*b.b; frc w;
	l=(m/a.b)*a.a-(m/b.b)*b.a;
	if (l>0) {
		w.a=l/__gcd(l,m);
		w.b=m/__gcd(l,m);
	} else {
		w.a=l; w.b=m;
	}
	return w;
}

frc operator*(const frc &a, const frc &b) {
	int l=a.a*b.a,m=a.b*b.b; frc w;
	if (l>0) {
		w.a=l/__gcd(l,m);
		w.b=m/__gcd(l,m);
	} else {
		w.a=l; w.b=m;
	}
	return w;
}

frc operator/(const frc &a, const frc &b) {
	frc c={b.b,b.a};
	return a*c;
}

bool operator==(const frc &a, const frc &b) {
	int m=a.b/__gcd(a.b,b.b)*b.b;
	return (m/a.b)*a.a==(m/b.b)*b.a;
}

bool operator<(const frc &a, const frc &b) {
	int m=a.b/__gcd(a.b,b.b)*b.b;
	return (m/a.b)*a.a<(m/b.b)*b.a;
}

bool operator>(const frc &a, const frc &b) {
	int m=a.b/__gcd(a.b,b.b)*b.b;
	return (m/a.b)*a.a>(m/b.b)*b.a;
}

pair <frc,frc> calc(frc tb, frc ta, frc t, frc s) {
	frc h=(s*(t-tb))/(ta-tb);
	return {s-h,h};
}

pair <frc,frc> merge(pair <frc,frc> a, pair <frc,frc> b) {
	pair <frc,frc> w;
	w.ss=a.ss+b.ss;
	w.ff=((a.ff*a.ss)+(b.ff*b.ss))/w.ss;
	return w;
}

const frc zero={0,1};
bool f;
int q,n,l,a,b,mxa,mna,mxb,mnb,sa,sb,x,y;
pair <frc,frc> h;
vector <pair<frc,frc>> t,w;

int main() {
	scanf("%d",&q);
	while (q--) {
		scanf("%d",&n);
		t.clear(); w.clear();
		sa=0; sb=0;
		mxa=0; mxb=0;
		mna=1000000; mnb=1000000;
		for (int i=0; i<n; ++i) {
			scanf("%d %d %d",&l,&a,&b);
			t.pb({{a,1},{l,1}}); w.pb({{b,1},{l,1}});
			sa+=l*a; sb+=l*b;
			mna=min(mna,a); mxa=max(mxa,a);
			mnb=min(mnb,b); mxb=max(mxb,b);
		}
		if (sa!=sb || mxa<mxb || mna>mnb) {printf("NIE\n"); continue;}
		sort(t.begin(),t.end());
		sort(w.begin(),w.end()); reverse(w.begin(),w.end());
		x=t.size()-1; y=x; f=1;
		for (auto i: w) {
			while (t[x].ss==zero&&x>0) {--x;} y=min(x,y);
			while (i.ff<t[y].ff&&y>0) {--y;}
			if (x==0&&t[x].ss==zero) {f=0; break;}
			if (x==y) {
				if (i.ss>t[x].ss) {
					f=0;
				} else {
					t[x].ss=t[x].ss-i.ss;
				}
			} else {
				while (f) {
					h=calc(t[y].ff,t[x].ff,i.ff,i.ss);
					if (h.ss>t[x].ss) {
						if (x==0) {
							f=0;
						} else {
							t[x-1]=merge(t[x],t[x-1]); --x;
						}
					} else {
						break;
					}
				}
				while (f) {
					h=calc(t[y].ff,t[x].ff,i.ff,i.ss);
					if (h.ff>t[y].ss) {
						if (y==0) {
								f=0;
						} else {
						t[y-1]=merge(t[y],t[y-1]); --y;
						}
						} else {
						break;
				}
				}
				if (!f) break;
			}
		}
		printf("%s\n",f?"TAK":"NIE");
	}
	return 0;
}