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
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int t, n;
long long a, b, a1, a2, b1, b2,wszt,mszt,b1n,l;
pair<long long, pair<long long, long long> > pa;
pair<long long, pair<long long, long long> > wi, mn;
priority_queue< pair<long long, pair<long long, long long> > > pl;
priority_queue< pair<long long, pair<long long, long long> > > pw;
int main()
{
	scanf("%d", &t);
	for(int i = 0; i < t; i++)
	{
		pl = priority_queue< pair<long long, pair<long long, long long> > >();
		pw = priority_queue< pair<long long, pair<long long, long long> > >();
		scanf("%d", &n);
		for(int j = 0; j< n; j++)
		{
			scanf("%lld%lld%lld", &l, &a, &b);
			if( b < a ){
				pa.first = b;
				pa.second.first = a;
				pa.second.second = l;
				pl.push(pa);
			}
			else if (b > a){
				pa.first = a;
				pa.second.first = b;
				pa.second.second = l;
				pw.push(pa);
			}

		}
		while(!pl.empty())
		{
			if(pw.empty())break;
			wi = pw.top();
			mn = pl.top();
			a1 = mn.second.first;
			a2 = mn.first;
			b1 = wi.first;
			b2 = wi.second.first;
			wszt = wi.second.second;
			mszt = mn.second.second;
			if(wi.first > mn.first) break;
			pl.pop();
			pw.pop();
			b1n = a1 + ((a2-b1)*b1-a1*(a2-b1))/(a1-b1);
			if(mszt > wszt){
				mn.second.second = mszt - wszt;
				pl.push(mn);
			}

			
			if(wszt > mszt){
				wi.second.second = wszt-mszt;
				pw.push(wi);
				wszt = mszt;
			}
			wi.second.second = wszt;
			if(b1n < b2){
				wi.first = b1n;
				pw.push(wi);
			}
			else if(b1n > b2){
				wi.first = b2;
				wi.second.first = b1n;
				pl.push(wi);
			}
		}
		if(!pl.empty() || !pw.empty()){
			printf("NIE\n");
		}
		else{
			printf("TAK\n");
		}
	}
	return 0;
}