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
100
101
102
103
#include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

using ll = long long;

struct K
{
    int V, T;
};

struct W
{
    ll V, E;
};

int main()
{
    ios_base::sync_with_stdio(false);
    int t, n, l, a, b;
    ll A, B;
    int aMax, aMin, bMax, bMin;
    vector<K> as, bs;
    cin >> t;
    while (t--)
    {
        aMax = -10000000;
        bMax = -10000000;
        aMin = 10000000;
        bMin = 10000000;
        A = 0;
        B = 0;
        cin >> n;
        as.resize(n);
        bs.resize(n);
        while (n--)
        {
            cin >> l >> a >> b;
            A += ll(l) * ll(a);
            B += ll(l) * ll(b);
            aMax = max(aMax, a);
            bMax = max(bMax, b);
            aMin = min(aMin, a);
            bMin = min(bMin, b);
            as[n] = {l, a};
            bs[n] = {l, b};
        }
        if (A == B && aMax >= bMax && aMin <= bMin)
        {
            sort(as.begin(), as.end(), [](K &a, K &b) {
                return a.T < b.T;
            });
            sort(bs.begin(), bs.end(), [](K &a, K &b) {
                return a.T < b.T;
            });
            auto asI = as.begin();
            W aW = {0, 0}, bW = {0, 0};
            bool bad = false;
            for (K &bx : bs)
            {
                bW.V += ll(bx.V);
                bW.E += ll(bx.V) * ll(bx.T);

                while (bx.V)
                {
                    if (asI->V <= bx.V)
                    {
                        bx.V -= asI->V;
                        aW.V += ll(asI->V);
                        aW.E += ll(asI->V) * ll(asI->T);
                        asI++;
                    }
                    else
                    {
                        aW.V += ll(bx.V);
                        aW.E += ll(bx.V) * ll(asI->T);
                        asI->V -= bx.V;
                        bx.V = 0;
                    }
                }

                //cout<<"Wiaderko A ma V="<<aW.V<<" i E="<<aW.E<<endl;
                //cout<<"Wiaderko B ma V="<<bW.V<<" i E="<<bW.E<<endl;

                if (aW.E > bW.E)
                {
                    //cout<<"BOO\n";
                    bad = true;
                    break;
                }
            }
            if (bad)
                cout << "NIE\n";
            else
                cout << "TAK\n";
        }
        else
            cout << "NIE\n";
    }
}