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
//Krzysztof Boryczka
#pragma GCC optimize "O3"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

#define FOR(i, b, e) for(int i=b; i<=e; i++)
#define FORD(i, b, e) for(int i=b; i>=e; i--)
#define SIZE(x) ((int)x.size())
#define pb push_back
#define st first
#define nd second
#define sp ' '
#define ent '\n'

const int N=1e5+5;
const ld EPS=1e-9;

int n;
pair<ld, ld> mam[N], dam[N];
ii m2[N], d2[N];
pair<ld, ld> akt;

void dolej(pair<ld, ld> x){
	if(akt.nd < EPS) akt={0.0, 0.0};
	if(x.nd < EPS) return;
	else akt={(akt.st*akt.nd+x.st*x.nd)/(akt.nd+x.nd), akt.nd+x.nd};
}

void solve(){
	int a, b, c;
	cin>>n;
	FOR(i, 1, n){
		cin>>a>>b>>c;
		m2[i]={b, a};
		d2[i]={c, a};
	}
	sort(m2+1, m2+n+1);
	sort(d2+1, d2+n+1);
	FOR(i, 1, n) mam[i]=m2[i];
	FOR(i, 1, n) dam[i]=d2[i];
	akt={0.0, 0.0};
	int pt=0;
	FOR(i, 1, n){
		while(pt<n && dam[i].nd > EPS){
			pt++;
			if(abs(mam[pt].st-dam[i].st) < EPS){
				ld mink=min(mam[pt].nd, dam[i].nd);
				mam[pt].nd-=mink;
				dam[i].nd-=mink;
				dolej(mam[pt]);
				continue;
			}
			if(mam[pt].st < dam[i].st){
				dolej(mam[pt]);
				continue;
			}
			if(akt.nd < EPS){
				break;
			}
			if(abs(dam[i].st-akt.st) < EPS){
				ld mink=min(akt.nd, dam[i].nd);
				akt.nd-=mink;
				dam[i].nd-=mink;
				if(akt.nd < EPS) akt={0.0, 0.0};
				pt--;
				continue;
			}
			ld prop=(dam[i].st-akt.st)/(mam[pt].st-dam[i].st);
			if(prop > EPS){
				ld leje=min({akt.nd, mam[pt].nd/prop, dam[i].nd/(prop+1.0)});
				dam[i].nd-=leje*(prop+1.0);
				akt.nd-=leje;
				mam[pt].nd-=leje*prop;
			}
			if(mam[pt].nd > EPS) pt--;
		}
		if(dam[i].nd > EPS){
			cout<<"NIE\n";
			return;
		}
	}
	cout<<"TAK\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int tt; cin>>tt;
	FOR(te, 1, tt)
	solve();
	return 0;
}