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
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int MAX = 100002;
int N;
pair<ll, ll> in[MAX];
pair<ll, ll> out[MAX];

void input(){
	cin >> N;
	int l, a, b;
	for(int i = 0; i < N; i++){
		cin >> l >> a >> b;
		in[i] = make_pair(a, l);
		out[i] = make_pair(b, l);
	}
}

bool check(){
	long long s = 0;
	for(int i = 0; i < N; i++){
		s += in[i].first * in[i].second;
		s -= out[i].first * out[i].second;
	}
	return s == 0;
}

bool process(){
	sort(in, in + N);
	sort(out, out + N);
	for(int i = 0; i < N - 1; i++){
		if(in[i].first == in[i + 1].first){
			in[i + 1].second += in[i].second;
			in[i].second = 0;
		}
		if(out[i].first == out[i + 1].first){
			out[i + 1].second += out[i].second;
			out[i].second = 0;
		}
	}
	int ind_out = N - 1;
	long long E = in[N - 1].first * in[N - 1].second;
	long long L = in[N - 1].second;
	for(int i = N - 2; i >= 0; i--){
			if(ind_out >= 0 && E < L * out[ind_out].first){
				//cout << "tutaj   E = " << E << "   L = " << L << endl;
				return false;
			}
		long long dE = in[i].first * in[i].second;
		long long dL = in[i].second;
		while(ind_out >= 0 && E + dE <= (L + dL) * out[ind_out].first){
			if(L + dL == 0){
				break;
			}
			//cout << "i = " << i << "   ind_out = " << ind_out << "    E = " << E << "    L = " << L << "   dE = " << dE << "   dL = " << dL << endl;
			//cout << "x = " << out[ind_out].first << "    y = " << out[ind_out].second << "          " << endl;
			if(dE == out[ind_out].first * dL){
				if(L + dL >= out[ind_out].second){
					E -= out[ind_out].first * out[ind_out].second;
					L -= out[ind_out].second;
					ind_out--;
				} else {
					return false;
				}
			} else {
				if(dL * (out[ind_out].first*L - E) <= (out[ind_out].second - L)*(dE - out[ind_out].first * dL)){
					E -= out[ind_out].first * out[ind_out].second;
					L -= out[ind_out].second;
					ind_out--;
				} else {
					return false;
				}
			}
		}
		E += dE;
		L += dL;
	}
	/*
	for(int ind_in = N - 1; ind_in >= 0; ind_in--){
		cout << "ind_in = " << ind_in << "   ind_out = " << ind_out << "    E = " << E << endl;
		while(ind_out >= 0 && out[ind_out].first > in[ind_in].first){
			E -= out[ind_out].first * out[ind_out].second;
			if(E < 0){
				cout << "error: " << ind_out << endl; 
				return false;
			}
			ind_out--;
		}
		E += in[ind_in].first * in[ind_in].second;
	}
	*/
	return true;
}

int main(){
	cin.tie(NULL);
	ios_base::sync_with_stdio(0);
	int T;
	cin >> T;
	for(int v = 0; v < T; v++){
		input();
		if(check()){
			if(process()){
				cout << "TAK" << endl;
			} else {
				cout << "NIE" << endl;
			}
		} else {
			cout << "NIE" << endl;
		}
	}	
}