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
#include <cstdio>
#include <cstdlib>

#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

int read() { int n; scanf("%d", &n); return n; }

const int MAXN = 100000 + 9;

typedef long long ll;

struct Node {
	ll temp;
	ll litr;

	bool operator<(const Node &oth) const {
		if (temp != oth.temp) return temp < oth.temp;
		return litr < oth.litr;
	}
};


bool solve() {
	int n = read();
	ll pota = 0;
	ll potb = 0;
	vector<Node> va(n);
	vector<Node> vb(n);
	for (int i=0; i<n; ++i) {
		ll l = read();
		ll a = read();
		ll b = read();
		pota += l * a;
		potb += l * b;
		va[i].litr = vb[i].litr = l;
		va[i].temp = a;
		vb[i].temp = b;
	}

	if (pota != potb) return false;

	sort(va.begin(), va.end());
	sort(vb.begin(), vb.end());

	ll l = 0;
	ll p = 0;

	int i = n - 1;

	for (int j=n-1; j>=0; --j) {
		ll tl = vb[j].litr;
		ll tt = vb[j].temp;

		while (l < tl) {
			ll l2 = l + va[i].litr;
			ll p2 = p + va[i].litr * va[i].temp;
			if (l2 <= tl) {
				l = l2;
				p = p2;
				--i;
				continue;
			}
			ll dl = tl - l;
			va[i].litr -= dl;
			l += dl;
			p += dl * va[i].temp;
		}

		if (p < l * tt) return false;
		p -= l * tt;
		l = 0;
	}

	return true;
}


int main() {

	int t = read();

	while (t--) {
		bool res = solve();
		printf("%s\n", res ? "TAK" : "NIE");
	}

	return 0;
}