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
#include <bits/stdc++.h>
#define ll long long
#define mk std::make_pair
#define st first
#define nd second

const int MAX_N = 1e5;
int n;
std::unordered_map<ll, ll> X, Y;
std::vector<std::pair<ll, ll> > A, B;
bool ok = true;

void input(){
    A.clear(); B.clear();
    X.clear(); Y.clear();
    ok = true;
    ll S1, S2; S1 = S2 = 0;
    std::cin >> n;
    ll x, y, z;
    for (int i = 1; i <= n; i++){
        std::cin >> x >> y >> z;
        S1 += x*y;
        S2 += x*z;
        X[y] += x;
        Y[z] += x;
    }
    for (auto it: X)
        A.push_back(it);
    for (auto it: Y)
        B.push_back(it);
    if (S1 != S2)
        ok = false;
}

void solve(){
    input();
    std::sort(A.begin(), A.end());
    std::sort(B.begin(), B.end());
    ll S = 0;
    while (!A.empty() || !B.empty()){
        ll r = A.back().st - B.back().st;
        ll l = std::min(A.back().nd, B.back().nd);
        S += r*l;
        if (S < 0)
            ok = false;
        A.back().nd -= l;
        B.back().nd -= l;
        if (A.back().nd == 0)
            A.pop_back();
        if (B.back().nd == 0)
            B.pop_back();
    }
    (ok) ? std::cout << "TAK\n" : std::cout << "NIE\n";
}

int main(){
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
    int t;
    std::cin >> t;
    while (t--)
        solve();
}