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
#include<bits/stdc++.h>
#define ll long long
#define st first
#define nd second
using namespace std;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		ll sum1=0, sum2=0;
		vector<ll> l(n), a(n), b(n);
		vector<pair<ll, ll>> v1, add;
		for(int i=0; i<n; ++i) {
			cin>>l[i]>>a[i]>>b[i];
			sum1+=l[i]*a[i];
			sum2+=l[i]*b[i];
			if(b[i]>a[i]) {
				v1.push_back({a[i], -l[i]});
				v1.push_back({b[i], l[i]});
			}
			else if(b[i]<a[i]) {
				v1.push_back({b[i], l[i]});
				v1.push_back({a[i], -l[i]});
			}
		}
		if(sum1!=sum2) {
			cout<<"NIE\n";
			continue;
		}
		sort(v1.begin(), v1.end());
		for(int i=0; i<(int)v1.size(); ++i) {
			if(i==0 || v1[i].st!=v1[i-1].st) {
				add.push_back(v1[i]);
			}
			else {
				add.back().nd+=v1[i].nd;
			}
		}
		ll sum=0, sweep=0;
		bool fail=0;
		for(int i=0; i<(int)add.size(); ++i) {
			if(i!=0) {
				sum+=(add[i].st-add[i-1].st)*sweep;
			}
			sweep+=add[i].nd;
			if(sum>0) {
				fail=1;
				break;
			}
		}
		if(fail) {
			cout<<"NIE\n";
		}
		else {
			cout<<"TAK\n";
		}
	}
	
}