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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

struct Acc {
    LL sum = 0;
    LL cnt = 0;
    bool operator==(Acc const&rhs) const { return sum == rhs.sum && cnt == rhs.cnt; };
    bool operator!=(Acc const&rhs) const { return !operator==(rhs);};

    void update(PII x) {
        sum += (LL)x.ST * x.ND;
        cnt += x.ND;
    }
    void update(Acc &x) {
        sum += x.sum;
        cnt += x.cnt;
    }
    void substract(Acc &x) {
        sum -= x.sum;
        cnt -= x.cnt;
    }
    void reset() {
        sum = 0;
        cnt = 0;
    }

};

bool check_in_single_direction(vector<PII> &a, vector<PII> &b, bool reversed) {
    Acc acc_a, acc_b, rest;
    int j=0, n=a.size();
    for(auto &bi: b) {
        acc_b.update(bi);
        acc_a.update(rest);
        while (j<n && acc_b.cnt > acc_a.cnt) {
            acc_a.update(a[j]);
            ++j;
        }
        rest.reset();
        rest.update(MP(a[j-1].ST, acc_a.cnt - acc_b.cnt));
        acc_a.substract(rest);
        if (!reversed && acc_a.sum > acc_b.sum) return false;
        if (reversed && acc_a.sum < acc_b.sum) return false;
    }
    return true;
}

bool read_and_solve(vector<PII> &a, vector<PII> &b) {
    Acc acc_a, acc_b;
    int n;
    scanf("%d", &n);
    REP(i, n) {
        int li, ai, bi;
        scanf("%d%d%d", &li, &ai, &bi);
        a.PB(MP(ai,li));
        b.PB(MP(bi,li));
        acc_a.update(MP(ai,li));
        acc_b.update(MP(bi,li));
    }
    if (acc_a != acc_b) return false;

    sort(begin(a), end(a));
    sort(begin(b), end(b));
    if (!check_in_single_direction(a, b, false)) return false;

    reverse(begin(a), end(a));
    reverse(begin(b), end(b));
    return check_in_single_direction(a, b, true);
}

int main() {
    vector<PII> a, b;
    a.reserve(100000);
    b.reserve(100000);
    int t;
    scanf("%d", &t);
    REP(tt, t) {
        a.clear();
        b.clear();
        printf(read_and_solve(a, b) ? "TAK\n" : "NIE\n");
    }
    return 0;
}