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
#include<bits/stdc++.h>
using namespace std;
 
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.first << ", " << p.second << ")";
}
 
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
	out << "{";
	for(auto it = a.begin(); it != a.end(); it = next(it))
		out << (it != a.begin() ? ", " : "") << *it;
	return out << "}";
}
 
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
	cerr << a << ", ";
	dump(x...);
}
 
#ifdef DEBUG
#  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#  define debug(...) false
#endif
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
template<class T> int size(T && a) { return a.size(); }
 
using LL = long long;
using PII = pair<int, int>;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int T;
	cin >> T;
	REP(it, T) {
		int n; cin >> n;
		vector<PII> s(n), t(n);
		REP(i, n) {
			int l, a, b;
			cin >> l >> a >> b;
			s[i] = {a, l};
			t[i] = {b, l};
		}
		sort(s.begin(), s.end());
		sort(t.begin(), t.end());

		LL q = 0;
		int pos = 0;
		bool ok = true;
		REP(i, n) {
			while(s[i].ND) {
				auto &a = s[i], &b = t[pos];
				int l = min(a.ND, b.ND);
				a.ND -= l, b.ND -= l;
				q += LL(b.ST - a.ST) * l;
				if(q < 0) ok = false;
				if(!b.ND) pos++;
			}
		}

		if(q == 0 && ok) cout << "TAK\n";
		else cout << "NIE\n";
	}
}