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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const ll INFTY=2000000000000ll;
const int MAX=500100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<tab[i]<<" ";
	}
	cout<<"\n";
}

template<class T> void coutVec(vector<T> tab){
	for(T t : tab){
		cout<<t<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
ll l[MAX];
ll a[MAX];
ll b[MAX];
int n;
const double EPS = 1e-6;
int comp(double a, double b) {
	if(abs(a-b) < EPS) return 0;
	return (a<b) ? -1 : 1;
}

set<int>::iterator last_lower_eq_than(set<int> & A, int a) {
	auto it = A.upper_bound(a);
	///ps("tryue ");pln((it==A.begin()));
	if(it == A.begin()) return A.end();
	it--;
	return it;
}

bool check() {
	map<int, double> cA; 
	map<int, double> cB;
	set<int> A;
	set<int> B; 
	loop(i,0,n) {
		cA[a[i]]+=l[i];
		A.insert(a[i]);
		cB[b[i]]+=l[i];
		B.insert(b[i]);
	}
	while(!A.empty() || !B.empty()) {
		int et = *B.rbegin();
		int t2 = *A.rbegin();
		set<int>::iterator it1;
		if((it1  = last_lower_eq_than(A, et)) == A.end()) return false; 
		int t1 = *it1;
		if(t2 < et) return false;
		//ps(t1);ps(t2);pln(et);
		//ps("test ");
		//for(auto i : A) ps(i);
		//entr;
		//for(auto i : B) ps(i);
		//entr;
		//entr;
		if(t1 == et) {
			double r = min(cA[t1], cB[et]);
			cA[t1] -= r;
			cB[et] -= r;
		} else if(t2 == et) {
			double r = min(cA[t2], cB[et]);
			cA[t2] -= r;
			cB[et] -= r;
		} else {
			double ax = cB[et]*(et - t2)/(t1-t2);
			ax = min(ax, cA[t1]);
			double bx = ax*(t1-et)/(et-t2);
			bx = min(bx, cA[t2]);
			ax = bx*(et-t2)/(t1-et);
			cA[t1] -= ax;
			cA[t2] -= bx;
			cB[et] -= (ax+bx);
		}
		if(comp(cA[t1],0) == 0) {
			A.erase(t1);
		}
		if(comp(cA[t2],0) == 0) {
			A.erase(t2);
		}
		if(comp(cB[et],0) == 0) {
			B.erase(et);
		}
	}
	if(!A.empty() || !B.empty()) return false;
	return true;
}

int main(){
	ios_base::sync_with_stdio(0);
	int t;
	cin>>t;
	while(t--) {
		cin>>n;
		loop(i,0,n) cin>>l[i]>>a[i]>>b[i];
		if(check()) pln("TAK");
		else pln("NIE");
	}
}