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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cassert>
#include <limits.h>

using namespace std;

#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef long long LL;
typedef long double LD;

typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)

const int MAXN=100000+5;

struct kubek {
    int l;
    int t;
};

int n;
int t;
kubek a[MAXN], b[MAXN];
int kubekCompare(const void * p1, const void * p2) {
    kubek * k1 = (kubek *) p1;
    kubek * k2 = (kubek *) p2;
    return k2->t - k1->t;
}

void printKubki(kubek *t, int n){
    REP(i, n) {
        printf("%d %d\n", t[i].l, t[i].t);
    }
}

bool solve() {
    SI(n);

    long long int at=0, bt=0;

    REP(i, n) {
        SI(a[i].l);
        b[i].l = a[i].l;
        SI(a[i].t);
        SI(b[i].t);

        at += ((long long) a[i].l) * a[i].t;
        bt += ((long long) b[i].l) * b[i].t;
    }

    qsort(a, n, sizeof(a[0]), kubekCompare);
    qsort(b, n, sizeof(b[0]), kubekCompare);
    
    if (a[0].t < b[0].t || a[n-1].t > b[n-1].t || at != bt) {
        return false;
    }

    int na=0, nb=0;
    FOR(i, 1, n-1) {
        if (a[na].t == a[i].t) {
            a[na].l += a[i].l;
        } else {
            na++;
            a[na] = a[i];
        }

        if (b[nb].t == b[i].t) {
            b[nb].l += b[i].l;
        } else {
            nb++;
            b[nb] = b[i];
        }
    }
    int ia=0, ib=0;
    while (ia <= na && ib <=nb) {
        if (a[ia].t == b[ib].t) {
            int c = min(a[ia].l, b[ib].l);
            a[ia].l -= c;
            b[ib].l -= c;
            ia++;
            ib++;
        } else if (a[ia].t > b[ib].t) {
            ia++;
        } else {
            ib++;
        }
    }

    ia=0;
    long double lu, lutu, lu1, lutu1;
    lu = a[0].l;
    lutu = a[0].l * a[0].t;
    ia++;
    FOR(ib, 0, nb) {
        if (b[ib].l) {
            while (ia<=na && (lutu - b[ib].t*lu > a[ia].l*(b[ib].t-a[ia].t))) {
                lutu += a[ia].l * a[ia].t;
                lu += a[ia].l;
                ia++;
            }
            
            if (ia<=na) {
                if (lutu - b[ib].t*lu > a[ia].l*(b[ib].t-a[ia].t)) {
                    return false;
                }
                if ((b[ib].t - a[ia].t) * (lu - b[ib].l) + lutu - b[ib].t*lu < 0) {
                    return false;
                }
                
                long double lx = ( lutu - lu*b[ib].t ) / (b[ib].t - a[ia].t);
                lu1 = a[ia].l - b[ib].l + lu;
                lutu1 = a[ia].t * (a[ia].l - lx) + b[ib].t * (lu + lx - b[ib].l);
                lu = lu1;
                lutu = lutu1;
                ia++;
            }
            
        }
    }

    return true;
}

int main() {
    SI(t);
    REP(ti, t) {
        if (solve()) {
            printf("TAK\n");
        } else {
            printf("NIE\n");
        }
    }

    return 0;
}