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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Artur Kraska, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007

using namespace std;

int n;
vector <int> v;
//list <pair<int ,int>> l;
map <int, long long> mapa; // long longi wszedzie?

bool dziala()
{
    long long przes = 0, suma = 0, last = -1;
    for(auto elem : mapa)
    {
        przes += suma * (elem.first - last);
        suma += elem.second;
        last = elem.first;

        if(przes < 0)
            return 0;

//        cout << "dla " << elem.first << ", " << elem.second << " mamy  przes " << przes << " i sume " << suma << endl;
    }
    if(suma != 0 || przes != 0)
        return 0;

    last = INF;
    FOREACHR(it, mapa)
    {
        auto elem = *it;
        przes += suma * (last - elem.first);
        suma += elem.second;
        last = elem.first;

        if(przes < 0)
            return 0;

//        cout << "dla " << elem.first << ", " << elem.second << " mamy  przes " << przes << " i sume " << suma << endl;
    }
    if(suma != 0 || przes != 0)
        return 0;

    return 1;
}

bool dziala2()
{
    int pocz = 0, kon = v.size()-1;
    while(pocz <= kon)
    {
        if(mapa.begin()->second < 0 || prev(mapa.end())->second < 0)
            return 0;
        while(mapa.begin()->second == 0)
            mapa.erase(mapa.begin());
        while(prev(mapa.end())->second == 0)
            mapa.erase(prev(mapa.end()));

        while(mapa[v[pocz]] >= 0 && pocz <= kon)
            pocz++;
        while(mapa[v[kon]] >= 0 && pocz <= kon)
            kon--;

        int ile = min(mapa.begin()->second, prev(mapa.end())->second);
        int diff = min(v[pocz] - mapa.begin()->first, prev(mapa.end())->first - v[kon]);

        mapa.begin()->second -= ile;
        prev(mapa.end())->second -= ile;
        mapa[mapa.begin()->second + diff] += ile;
        mapa[prev(mapa.end())->second - diff] += ile;
    }

    return 1;
}

int main()
{
    testy()
    {
        mapa.clear();
        //l.clear();
        scanf("%d", &n);
        forr(i, n)
        {
            int ob, t1, t2;
            scanf("%d %d %d", &ob, &t1, &t2);
            //v.PB(t2);
            mapa[t1] += ob;
            mapa[t2] -= ob;
            //v.PB(MP(t1, ob));
            //v.PB(MP(t2, -ob));
        }
        //sort(v.begin(), v.end());

        printf("%s\n", dziala() ? "TAK" : "NIE");

        /*
        int poz = 0;
        FOR(i, 1, 2*n-1)
        {
            if(v[poz].first == v[i].first)
                v[poz].second += v[i].second;
            else
            {
                poz++;
                v[poz].first = v[i].first;
                v[poz].second = v[i].second;
            }
        }
        FOR(i, 0, poz)
        {
            cout << "dla temperatury " << v[i].first << " mamy " << v[i].second << endl;
        }

        for(auto p : v)
            l.PB(p);
        while(next(l.begin()) != l.end())
        {
            auto pocz = l.front(), kon = l.back();
        }*/


    }

    return 0;
}