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
#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>

using namespace std;

void test_case() {
	int n, i, j;
	int64_t m, b, a, x, y, x2, y2, xa, ya;
	vector<pair<int64_t, int64_t> > before, after;
	scanf("%d", &n);
	before.reserve(n + 8);
	after.reserve(n + 8);
	for (i = 0; i < n; ++i) {
		scanf("%lld %lld %lld", &m, &b, &a);
		before.push_back(make_pair(b, m));
		after.push_back(make_pair(a, m));
	}
	sort(before.rbegin(), before.rend());
	sort(after.rbegin(), after.rend());
	xa = 0;
	ya = 0;
	x = 0;
	y = 0;
	j = 0;
	for(i = 0; i < n; ++i) {
		x2 = x + before[i].second;
		y2 = y + before[i].first * before[i].second;
		while (j < n && xa + after[j].second <= x2) {
			xa += after[j].second;
			ya += after[j].first * after[j].second;
			if ((ya - y) * (x2 - x) > (y2 - y) * (xa - x)) {
				printf("NIE\n");
				return;
			}
			++j;
		}
		x = x2;
		y = y2;
	}
	printf(y == ya ? "TAK\n" : "NIE\n");
}

int main () {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		test_case();
	}
	return 0;
}