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
#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;

struct her {
	lli v, t;
};

bool operator<(her a, her b) {
	return a.t < b.t;
}

vector<her> v1,v2;

void NIE() {
	printf("NIE\n");
}

void TAK() {
	printf("TAK\n");
}

void solve() {
	int n;
	scanf("%d", &n);
	lli v,a,b;
	lli sa = 0, sb = 0;
	v1.clear();
	v2.clear();
	
	for (int i=0; i<n; i++) {
		scanf("%lld%lld%lld", &v, &a, &b);
		v1.push_back(her({v,a}));
		v2.push_back(her({v,b}));
		
		sa += v*a;
		sb += v*b;
	}
	
	if (sa != sb)
		return NIE();
	
	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	
	sa = 0;
	sb = 0;
	
	while (!v1.empty()) {
		auto x1 = v1.back();
		v1.pop_back();
		auto x2 = v2.back();
		v2.pop_back();
		
		lli s = min(x1.v, x2.v);
		
		if (x1.v != s)
			v1.push_back(her({x1.v-s, x1.t}));
		if (x2.v != s)
			v2.push_back(her({x2.v-s, x2.t}));
		
		sa += s*x1.t;
		sb += s*x2.t;
		
		if (sa < sb)
			return NIE();
	}
	
	return TAK();
}

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