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
#include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <cmath>
#include <limits>
using namespace std;
using LLI = long long int;
using PLD = pair<double, double>;

int t;

int cmp(double a, double b) {
    if(fabs(a - b) <= 1e-5)     
       return 0;
    else if(a < b)
        return -1;
    return 1;
}

bool coolDown(vector<PLD>& cups, PLD& goal) {
    if(cmp(cups.back().first, goal.first) == -1)
        return false;

    PLD coolingCup = cups.back();
    cups.pop_back();

    while(!cups.empty() && cmp(coolingCup.first, goal.first) != 0) {
        PLD mixingCup = cups.back();

        bool all = false;
        double howMuch = 0;

        if(cmp(goal.first, mixingCup.first) == 0)
            all = true;
        else {
            double toDrain = (coolingCup.first * coolingCup.second - goal.first * coolingCup.second) / (goal.first - mixingCup.first);
            if(cmp(toDrain, 0) <= 0 || cmp(toDrain, mixingCup.second) >= 0)
                all = true;
            else
                howMuch = toDrain;
        }

        if(all) {
            double pom = (coolingCup.first * coolingCup.second + mixingCup.first * mixingCup.second) / (coolingCup.second + mixingCup.second);
            coolingCup.first = pom;
            coolingCup.second += mixingCup.second;
            mixingCup.second = 0;
            cups.pop_back();
        }
        else {
            coolingCup.first = (coolingCup.first * coolingCup.second + mixingCup.first * howMuch) / (coolingCup.second + howMuch);
            coolingCup.second += howMuch;
            mixingCup.second -= howMuch;
            cups.back().second -= howMuch;
            if(cmp(cups.back().second, 0) == 0)
                cups.pop_back();
        }
    }   

    if(cmp(coolingCup.first, goal.first) != 0) {
        return false;
    }
    
    if(cmp(coolingCup.second, goal.second) == 1) {
        coolingCup.second -= goal.second;
        cups.push_back(coolingCup);
        goal.second = 0;

        return true;
    }
    else {
        goal.second -= coolingCup.second;

        return true;
    }
}

bool isAchieveable() {
    int n;
    vector<PLD> goal{};
    vector<PLD> cups{};
    double sum1 = 0, sum2 = 0;

    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        double a;
        double b, c;
        scanf("%lf%lf%lf", &a, &b, &c);
        goal.push_back(make_pair(c, a));
        cups.push_back(make_pair(b, a));
        sum1 += a * b;
        sum2 += a * c;
    }

    if(sum1 != sum2) {
        return false;
    }

    sort(goal.begin(), goal.end());
    sort(cups.begin(), cups.end());

    while(!goal.empty()) {
        if(cups.empty() || !coolDown(cups, goal.back())) {
            return false;
        }

        if(cmp(goal.back().second, 0) == 0) {
            goal.pop_back();
        }
    }

    return true;    
}

int main() {
    scanf("%d", &t);
    while(t--) {
        if(isAchieveable())
            printf("TAK\n");
        else
            printf("NIE\n");
    }
}