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>
#pragma GCC optimize ("-O3")
#define io cin.sync_with_stdio(0);cin.tie(0); \
	cin.exceptions(cin.failbit);
using namespace std;    using ll=long long;
using pii=pair<int,int>;using vi=vector<int>;
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define ROF(i,a,b) for(int i=a;i>=b;--i)
#define VEC(i,v)   for(auto&i:v)
#define I(x) ({x t;cin>>t;t;})
#define sz(x) int(x.size())
#define oo INT_MAX
#define pf printf

struct kubek {
	int k;
	int t;
};

int main() { io;
	int t = I(int);
	for (int T = 0; T < t; T++) {
		int n = I(int);
		int status = 1;
		ll Q = 0;

		deque<kubek> V, R;

		for (int i = 0; i < n; i++) {
			int k = I(int), t1 = I(int), t2 = I(int);
			V.push_back({k, t1});
			R.push_back({k, t2});
		}

		sort(V.begin(), V.end(),
			[](const kubek &a, const kubek &b) -> bool
			{ return a.t > b.t; });
		sort(R.begin(), R.end(),
			[](const kubek &a, const kubek &b) -> bool
			{ return a.t > b.t; });

		while (!V.empty()) {
			kubek x = V.front(); V.pop_front();
			kubek y = R.front(); R.pop_front();
			
			if (x.t >= y.t) {
				int h = x.t - y.t;
				Q += h * x.k;
				if (x.k > y.k) {
					V.push_front({x.k - y.k, y.t});
				} else {
					R.push_front({y.k - x.k, y.t});
				}
			} else {
				int h = y.t - x.t;
				if (x.k > y.k) {
					V.push_front({x.k - y.k, x.t});
					Q -= y.k * h;
				} else {
					R.push_front({y.k - x.k, y.t});
					Q -= x.k * h;
				}
			}

			if (Q < 0) {
				status = -1;
				break;
			}
		}

		if (status == -1 || Q != 0) {
			pf("NIE\n");
		} else {
			pf("TAK\n");
		}
	}

	return 0;
}