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

using namespace std;

struct cup
{
    long long temp, volume;
    cup () {}
    cup (const long long& a, const long long& b) : temp(a), volume(b) {}
    bool operator<(const cup& a) const { return temp < a.temp; }
};

bool solve(vector<cup>& in, vector<cup>& out)
{
    vector<long long> index;
    long long in_width = 0, out_width = 0, deficit = 0;

    for (vector<cup>::iterator i = in.begin(), j = out.begin(); i != in.end() || j != out.end();)
    {
        if (i == in.end())
        {
            out_width += j->volume;
            index.push_back(out_width);
            j++;
        }
        else if (j == out.end())
        {
            in_width += i->volume;
            index.push_back(in_width);
            i++;
        }
        else if (in_width + i->volume < out_width + j->volume)
        {
            in_width += i->volume;
            i++;
            index.push_back(in_width);
        }
        else
        {
            out_width += j->volume;
            index.push_back(out_width);
            j++;
        }
    }
    index.resize(unique(index.begin(), index.end()) - index.begin());

    vector<cup>::iterator i = in.begin(), j = out.begin();
    in_width = out_width = 0;
    long long old = 0;
    for (vector<long long>::iterator at = index.begin(); at != index.end(); at++)
    {
        if (in_width + i->volume < *at)
        {
            in_width += i->volume;
            ++i;
        }
        if (out_width + j->volume < *at)
        {
            out_width += j->volume;
            ++j;
        }
        
        deficit -= (*at - old) * i->temp;
        deficit += (*at - old) * j->temp;

        if (deficit < 0)
            return false;
        //cout << deficit << ' ' << *at << ' ' << old << ' ' << i - in.begin() << ' ' << j - out.begin() << endl;

        old = *at;
    }
    return !deficit;
}

void exec()
{
    int n; cin >> n;
    vector<cup> in, out;
    for (int i = 0; i < n; i++)
    {
        int v, a, b; cin >> v >> a >> b;
        in.push_back(cup(a, v));
        out.push_back(cup(b, v));
    }
    sort(in.begin(), in.end());
    sort(out.begin(), out.end());

    cout << (solve(in, out) ? "TAK\n" : "NIE\n");
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int z; cin >> z;
    while (z--)
        exec();
}