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
#include <iostream>
#include <queue>

std::priority_queue<std::pair<long long, long long>, std::vector<std::pair<long long, long long>>, std::greater<std::pair<long long, long long>>> stare, nowe;
std::vector<long long> tab;

void f() {
    tab.clear();
    while (!stare.empty())
        stare.pop();
    while (!nowe.empty())
        nowe.pop();
    int n;
    scanf("%d", &n);
    int a, b, c;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d%d", &a, &b, &c);
        stare.push({b, a});
        nowe.push({c, a});
    }
    stare.push({1000000009, 1});
    nowe.push({1000000009, 1});
    while (stare.top().first != 1000000009 || nowe.top().first != 1000000009) {
        auto now = nowe.top();
        auto star = stare.top();
        nowe.pop();
        stare.pop();
        if (now.second == star.second) {
            tab.push_back(now.second * (now.first - star.first));
        } else if (now.second < star.second) {
            stare.push({star.first, star.second - now.second});
            tab.push_back(now.second * (now.first - star.first));
        } else if (now.second > star.second) {
            nowe.push({now.first, now.second - star.second});
            tab.push_back(star.second * (now.first - star.first));
        }
    }
    long long suma = 0;
    for (auto it : tab) {
        suma += it;
        if (suma < 0) {
            printf("NIE\n");
            return;
        }
    }
    printf("TAK\n");
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        f();
    }
    return 0;
}