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
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)

typedef long long LL;
typedef pair<int,int> PII;
typedef vector<PII> VII;

bool go() {
	INT(n);
	VII a, b;
	a.reserve(n);
	b.reserve(n);
	REP(i,n) {
		INT(l);
		INT(aa);
		INT(bb);
		a.PB(MP(aa, l));
		b.PB(MP(bb, l));
	}
	sort(ALL(a));
	sort(ALL(b));
	LL s = 0;
	while (!a.empty() && !b.empty()) {
		PII& aa = a.back();
		PII& bb = b.back();
		int l = min(aa.SD, bb.SD);
		s += LL(aa.FT - bb.FT) * l;
		if (s < 0) return 0;
		if (!(aa.SD -= l)) a.pop_back();
		if (!(bb.SD -= l)) b.pop_back();
	}
	return !s;
}

int main() {
	INT(t);
	REP(tt,t) printf(go() ? "TAK\n" : "NIE\n");
}