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
// TEMPLATE START

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

const int MOD = 1000000007;
const int INF = 2147483647;
const ll LINF = 1e18;
const ld PI = 4*atan((ld)1);

#define rep(i,a,b) for (int i=(a); i<(b); ++i)
#define repd(i,a,b) for (int i=((int)(a)); i>=(b); --i)
#define trav(a,x)  for (auto& a : x)
#define all(u) begin(u), end(u)
#define sz(x) (int)x.size()
#define xsort(v, m) rep(i, 0, v.size()) m[i] = i; sort(all(m), [=](int i, int j) {return v[i] < v[j];}); sort(v);

#define pb push_back
#define X first
#define Y second

#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PREC(n) cout<<fixed<<setprecision(n);

#define db(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define dbv(v) cerr << __LINE__ << ": " << #v << "\n" << v.size() << "\n" << v << "\n";


template <class T, class S> ostream& operator << (ostream& os, const pair<T, S>& p) {
        os << p.X << " " << p.Y;
    return os;
}

template <class T, class S> istream& operator >> (istream& is,  pair<T, S>& p) {
        is >> p.X >> p.Y;
    return is;
}

template <class T> ostream& operator << (ostream& os, const vector<T>& v) {
    rep(i, 0, v.size())
        os << v[i] << " \n" [i == v.size() - 1];
    return os;
}

template <class T> istream& operator >> (istream& is,  vector<T>& v) {
    rep(i, 0, v.size())
        is >> v[i];
    return is;
}


// TEMPLATE END


// SOLUTION START

bool prefix(vector<pair<ll, ll>> act, vector<pair<ll, ll>> exp, int sgn) {
    ll sum = 0, ltr = 0;
    int p = 0;
    trav(x, exp) {
        sum += x.X * x.Y;
        ltr += x.Y;
        while(p < sz(act)) {
            if(act[p].Y > ltr) {
                act[p].Y -= ltr;
                sum -= act[p].X * ltr;
                ltr = 0;
                break;
            }
            else {
                ltr -= act[p].Y;
                sum -= act[p].X * act[p].Y;
                p++;
            }
        }
        if(sgn * sum < 0) {
            return false;
        }
    }
    return true;
}

int main() {
    FAST_IO
	int n, t;
    cin >> t;
    while(t--) {
        cin >> n;
        vector<pair<ll, ll>> exp(n), act(n);
        rep(i, 0, n) {
            cin >> act[i].Y >> act[i].X >> exp[i].X;
            exp[i].Y = act[i].Y;
        }
        bool r = true;
        sort(exp.begin(), exp.end());
        sort(act.begin(), act.end());
        r = r && prefix(act, exp, 1);
        reverse(all(exp));
        reverse(all(act));
        r = r && prefix(act, exp, -1);
        cout << (r ? "TAK" : "NIE") << "\n";
    }
    return 0;
}

// SOLUTION END