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
#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
#define int long long

vector<pii> mam;
vector<pii> chce;
int n;

bool solve()
{
    int ile_litrow = 0;
    int srednia_moja = 0;
    int srednia_jego = 0;
    for (int i = n - 1; i >= 0; i--)
    {
        ile_litrow += mam[i].nd;
        srednia_moja += mam[i].nd * mam[i].st;
        while (ile_litrow != 0)
        {
            if (chce.back().nd > ile_litrow)
            {
                srednia_jego += ile_litrow * chce.back().st;
                chce.back().nd -= ile_litrow;
                ile_litrow = 0;
            }
            else
            {
                srednia_jego += chce.back().nd * chce.back().st;
                ile_litrow -= chce.back().nd;
                chce.pop_back();
            }
        }
        if ((srednia_moja > srednia_jego) || (i == 0 && srednia_moja != srednia_jego))
        {
            //cerr << srednia_moja << " " << srednia_jego << " " << i << endl;
            return false;
        }
    }
    return true;
}

void pre()
{
    cin >> n;
    mam.resize(n);
    chce.resize(n);
    rep(i, n)
    {
        int a, b, c;
        cin >> a >> b >> c;
        mam[i] = {c, a};
        chce[i] = {b, a};
    }
    sort(all(chce));
    sort(all(mam));
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    while (Z--)
    {
        pre();
        // cerr << solve();
        cout << (solve() ? "TAK" : "NIE") << endl;
    }
}