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
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
using namespace std;

// to jest debug() << imie(a) << imie(b) << imie((char)c);
#define R22(r) template <class c> typename enable_if<sizeof(dud<c>(0)) r,muu&>::type operator<<(c g)
template <class c> struct rge {c b, e;};
template <class c> rge<c> range(c i, c j) {return rge<c>{i, j};}
template <class c> auto dud(c*r)->decltype(cerr << *r);
template <class c> char dud(...);
struct muu {
	#ifdef LOCAL
	stringstream a;
	~muu() {cerr << a.str() << endl;}
	R22(>=2) {a << boolalpha << g; return *this;}
	R22(==1) {return *this << range(begin(g), end(g));}
	template <class c > muu & operator<<( rge<c> u) {
		a << "[";
		for (c i = u.b; i != u.e; ++i)
			*this << ", " + 2 * (i == u.b) << *i;
		return *this << "]";
	}
	template <class c, class m > muu & operator<<( pair <m,c> r) {return *this << "(" << r.first << ", " << r.second << ")";}
	#else
	template <class c > muu & operator<<( const c&){return *this;}
	#endif
	muu & operator()(){return *this;}
};
#define imie(r) "[" #r ": " << (r) << "] "
#define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ")

// to jest dodawanie par, mozna ominac
template<typename T, typename S> pair<T, S> operator+(pair<T,S> a, pair<T,S>b) {return {a.first+b.first, a.second+b.second};}
template<typename T> T& mini(T& a, T b) { return b < a ? a=b : a; }
template<typename T> T& maxi(T& a, T b) { return b > a ? a=b : a; }

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pld = pair<ld, ld>;
using pii = pair<int, int>;

const int MAXN = 1e5+69;
vector<pair<int, ll>> a, b;
int main() {
	int tt;
	scanf("%d", &tt);
	while (tt--) {
		debug() << "_________";
		a.clear();b.clear();
		int n;
		scanf("%d", &n);
		ll s = 0;
		for (int i = 0; i < n; i++) {
			int t;
			int aa,bb;
			scanf("%d%d%d", &t, &aa, &bb);
			s += ((ll)(aa-bb))*t;
			a.emplace_back(aa, t);
			b.emplace_back(bb, t);
		}
		if (s != 0) {
			debug() << imie(s);
			printf("NIE\n");
			continue;
		}
		sort(a.begin(), a.end());
		sort(b.begin(), b.end());

		ll rozn = 0;
		while (a.size() && b.size()) {
			auto &ai = a.back(), &bi = b.back();
			int ile = min(ai.second, bi.second);
			ai.second -= ile;
			bi.second -= ile;
			rozn += ((ll)ile)*(ai.first - bi.first);
			if (rozn < 0) {
				debug() << imie(rozn);
				break;
			}
			if (!ai.second) a.pop_back();
			if (!bi.second) b.pop_back();
		}

		puts((a.size() || b.size()) ? "NIE" : "TAK");
	}

}