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
#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include <vector>

using namespace std;
typedef long long LL;

string swap_cups(LL mini, LL maxi, LL mini_new, LL maxi_new, LL sumbitry, LL sum1, LL sum2) {
	if (mini_new < mini) 
		return "NIE";

	if(maxi_new > maxi) 
		return "NIE";

	if (sum1 == sum2) {
		return "TAK";
	}
	else {
		return "NIE";
	}
}

bool checkPrefixes(vector<pair<LL, LL>>& allOld, vector<pair<LL, LL>>& allNew) {
	LL prefixOld = 0, prefixNew = 0;
	for (int i=0; i<allOld.size(); i++) {
		prefixOld += allOld[i].second;
		prefixNew += allNew[i].second;
		if (prefixOld > prefixNew) {
			return true;
		}
	}
	return false;
}

bool checkSuffixes(vector<pair<LL, LL>>& allOld, vector<pair<LL, LL>>& allNew) {
	LL suffixOld = 0, suffixNew = 0;
		for (int i=allOld.size()-1; i>=0; i--) {
		suffixOld += allOld[i].second;
		suffixNew += allNew[i].second;
		if (suffixOld < suffixNew) {
			return true;
		}
	}
	return false;
}

int main() {
	std::ios::sync_with_stdio(false);
	int z;
	cin >> z;
	while (z--) {
		LL height, no, year, k, tmp, a, b, c;
		cin >> no;
		vector<LL> bitry;
		vector<LL> oldtemp;
		vector<LL> newtemp;
		vector<pair<LL, LL>> allOld;
		vector<pair<LL, LL>> allNew;
		for (int i = 0; i < no; i++) {
			cin >> a >> b >> c;
			bitry.push_back(a);
			oldtemp.push_back(b);
			newtemp.push_back(c);
			allOld.push_back(make_pair(a,b));
			allNew.push_back(make_pair(a,c));
		}
		std::sort(allOld.begin(), allOld.end());
		std::sort(allNew.begin(), allNew.end());
		
		LL sum1 = 0;
		LL sum2 = 0;
		LL sumbitry = 0;
		LL mini = oldtemp[0], mini_new = oldtemp[0];
		LL maxi = oldtemp[0], maxi_new = oldtemp[0];
		LL prefixOld = 0, prefixNew = 0;
		LL suffixOld = 0, suffixNew = 0;
		
        for (int i = 0; i < no; i++) {
			mini = std::min(oldtemp[i], mini);
			maxi = std::max(oldtemp[i], maxi);	
			sumbitry += bitry[i];		
			sum1 += bitry[i] * oldtemp[i];
			sum2 += bitry[i] * newtemp[i];
		}

		for (int i=0; i<no; i++) {
			mini_new = std::min(newtemp[i], mini_new);
			maxi_new = std::max(newtemp[i], maxi_new);
		}

		sum1 = sum1 / sumbitry;
		sum2 = sum2 / sumbitry;

		if (sumbitry == no) {
			if(checkPrefixes(allOld, allNew) || checkSuffixes(allOld, allNew)) {
				cout << "NIE" <<  endl;
			} else {
				cout << swap_cups(mini, maxi, mini_new, maxi_new, sumbitry, sum1, sum2) << endl;
			}
		} else {
			cout << swap_cups(mini, maxi, mini_new, maxi_new, sumbitry, sum1, sum2) << endl;
		}
	}
}