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
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100005;

int n, l[MAX_N], a[MAX_N], b[MAX_N];
vector<pair<int, int>> initial, goal;
long long greaterProd[MAX_N], greaterA[MAX_N], lowerProd[MAX_N], lowerA[MAX_N];
long long sumPrefProdA[MAX_N], sumPrefA[MAX_N];

void test() {
    initial.clear();
    goal.clear();
    scanf("%d", &n);
    initial.push_back({-1, 0});
    goal.push_back({-1, 0});
    for (int i = 1; i <= n; i++) {
        scanf("%d %d %d", &l[i], &a[i], &b[i]);
        initial.push_back({a[i], l[i]});
        goal.push_back({b[i], l[i]});
    }
    sort(initial.begin(), initial.end());
    sort(goal.begin(), goal.end());
    
    for (int i = 1; i < initial.size(); i++) {
        sumPrefProdA[i] = sumPrefProdA[i - 1] + (long long)initial[i].first * initial[i].second;
        sumPrefA[i] = sumPrefA[i - 1] + initial[i].second;
    }
    sumPrefA[n + 1] = sumPrefA[n];
    sumPrefProdA[n + 1] = sumPrefProdA[n];
    
    long long sumProdB = 0;
    long long sumB = 0;
    for (int i = 1; i < goal.size(); i++) {
        sumProdB += goal[i].first * (long long)goal[i].second;
        sumB += goal[i].second;
        int low = 1;
        int high = n;
        int res = 0;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (initial[mid].first * sumB <= sumProdB) {
                res = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        if (res == 0) {
            printf("NIE\n");
            return;
        }
        long long take = min(sumB, sumPrefA[res]);
        
        if (take == sumB) continue;
        
        low = res + 1;
        high = n;
        int w = n;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (sumPrefA[mid] - sumPrefA[res] >= sumB - take) {
                w = mid;
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        
        long long PL = sumPrefProdA[res];
        long long PR = sumPrefProdA[w - 1] - sumPrefProdA[res];
        long long T0 = initial[w].first;
        long long V = sumB;
        long long VL = take;
        long long VR = sumPrefA[w - 1] - sumPrefA[res];
        long long SB = sumProdB;
        
        if (PL + PR + T0 * (V - VL - VR) > SB) {
            printf("NIE\n");
            return;
        }
    }
    
    sumProdB = 0;
    sumB = 0;
    for (int i = goal.size() - 1; i >= 1; i--) {
        sumProdB += goal[i].first * (long long)goal[i].second;
        sumB += goal[i].second;
        int low = 1;
        int high = n;
        int res = 0;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (initial[mid].first * sumB < sumProdB) {
                res = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        if (res == n) {
            printf("NIE\n");
            return;
        }
        long long take = min(sumB, sumPrefA[n] - sumPrefA[res]);
        if (take == sumB) 
            continue;
        low = 1;
        high = res;
        int w = 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (sumPrefA[res] - sumPrefA[mid - 1] >= sumB - take) {
                w = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        
        long long PL = sumPrefProdA[n] - sumPrefProdA[res];
        long long PR = sumPrefProdA[res] - sumPrefProdA[w];
        long long T0 = initial[w].first;
        long long V = sumB;
        long long VL = take;
        long long VR = sumPrefA[res] - sumPrefA[w];
        long long SB = sumProdB;
        
        if (PL + PR + T0 * (V - VL - VR) < SB) {
            printf("NIE\n");
            return;
        }
        
    }
    printf("TAK\n");
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        test();
    }
    
    return 0;
}