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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

void solve() {
	int n;
	scanf("%d", &n);
	ll total_a = 0, total_b = 0;
	vector<pii> va, vb;
	for (int i = 0; i < n; i++) {
		int l,a,b;
		scanf("%d%d%d", &l, &a, &b);
		total_a += (ll)l*a;
		total_b += (ll)l*b;
		va.push_back({a,l});
		vb.push_back({b,l});
	}
	if (total_a != total_b) {
		puts("NIE");
		return;
	}
	sort(va.begin(), va.end(), greater<pair<int,int>>());
	sort(vb.begin(), vb.end(), greater<pair<int,int>>());
	ll la = 0, lb = 0;
	ll sa = 0, sb = 0;
	int wska = 0, wskb = 0;
	while (wska < n) {
		if (la + va[wska].second <= lb + vb[wskb].second) {
			la += va[wska].second;
			sa += (ll)va[wska].first*va[wska].second;
			wska++;
			if (sa < sb + (ll)vb[wskb].first*(la-lb)) {
				puts("NIE");
				return;
			}
		} else {
			lb += vb[wskb].second;
			sb += (ll)vb[wskb].first*vb[wskb].second;
			wskb++;
			if (sa + (ll)va[wska].first*(lb-la) < sb) {
				puts("NIE");
				return;
			}
		}
	}
	puts("TAK");
}

int main() {
	int tt;
	scanf("%d", &tt);
	while (tt--) {
		solve();
	}
}