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

using namespace std;
const int mx = 1e6+10;

long long velA[mx], velB[mx];
vector < int > vecA, vecB;

void clr(){

    for(auto i : vecA){
		velA[i] = 0;
	}
	for(auto i : vecB){
		velB[i] = 0;
	}
    vecA.clear();
    vecB.clear();
}

bool RES(){
    int n;
    scanf("%d",&n);
    clr();
    
    long long sumA = 0, sumB = 0;
    for(int i=0; i<n; i++){
        int l, a, b;
        scanf("%d %d %d",&l,&a,&b);
        velA[a] += l;
        velB[b] += l;
        sumA += a*l;
        sumB += b*l;
        vecA.push_back(a);
        vecB.push_back(b);
    }

    if(sumA != sumB) return false;
    
    sort(vecA.begin(), vecA.end());
	vecA.erase(unique(vecA.begin(), vecA.end()), vecA.end());
	sort(vecB.begin(), vecB.end());
	vecB.erase(unique(vecB.begin(), vecB.end()), vecB.end());

    int iA = vecA.size()-1, iB = vecB.size() - 1;
    sumA = 0;
    while(iA >= 0 or iB >= 0){
		long long rV = min(velA[vecA[iA]], velB[vecB[iB]]), rT = vecA[iA] - vecB[iB];
		velA[vecA[iA]] -= rV;
		velB[vecB[iB]] -= rV;
		sumA += rT * rV;

		if(sumA < 0) return false;

		if(velA[vecA[iA]] <= 0){
			iA --;
		}
		if(velB[vecB[iB]] <= 0){
			iB --;
		}
    }
    return sumA == 0;
}
int main(){
    int T;
    scanf("%d",&T);

    while(T--){

        if(RES()) puts("TAK");
        else puts("NIE");
    }

    return 0;
}