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
#include <cstdio>
#include <tuple>
#include <vector>
#include <algorithm>
using namespace std;

void yesno(int n) {
    vector<tuple<int, int, int>> dec;
    vector<tuple<int, int, int>> inc;
    auto bank = make_tuple (0, 0); //Temperatura, energia
    bool yes = true;
    int l, a, b;
    int mina = 1000001;
    int minb = 1000001;
    int maxa = 0;
    int maxb = 0;
    int totala = 0;
    int totalb = 0;


    for (int i = 0; i < n; ++i) {
        scanf("%d %d %d", &l, &a, &b);

        if (a < mina) mina = a;
        if (b < minb) minb = b;
        if (a > maxa) maxa = a;
        if (b > maxb) maxb = b;
        totala += a * l;
        totalb += b *l;

        if (a > b) dec.emplace_back(a, l, b);
        else if (a < b) inc.emplace_back(b, l, a);
    }
    if (minb < mina || maxb > maxa || totala != totalb) {
        yes = false;
    }


    sort(dec.begin(), dec.end());
    sort(inc.begin(), inc.end());

    int deci = 0;


    for (int inci = 0; inci < inc.size(); ++inci) {
        if (get<1>(bank) == 0){
            if (deci >= dec.size()) {

                yes = false;
                break;
            }
            else {
                bank = make_tuple(get<0>(dec[deci]), get<1>(dec[deci]) * (get<0>(dec[deci]) - get<2>(dec[deci])) );
                deci++;
            }
        }

        int banke = get<1>(bank);
        int bankt = get<0>(bank);

        if (get<0>(inc[inci]) > bankt) {
            yes = false;
            break;
        }

        int potrzeba = get<1>(inc[inci]) * (get<0>(inc[inci]) - get<2>(inc[inci]));


        if (banke >= potrzeba ) { // Wystarcza energii w banku
            bank = make_tuple(bankt, banke - potrzeba);

        }

        else {
            int nexte = (get<1>(dec[deci]) * (get<0>(dec[deci]) - get<2>(dec[deci])));
            bank = make_tuple((banke * bankt + get<0>(dec[deci]) * nexte) / (banke + nexte)  , banke + nexte);
            deci++;
            inci--;
        }




    }


    if (yes) printf("%s\n", "TAK");
    else printf("%s \n", "NIE");

}

int main() {
  int t;
    scanf("%d", &t);
    int n;

    while (t > 0) {
        scanf("%d", &n);
        yesno(n);
        t--;
    }


    return 0;
}

/* 5
2 2 1 4 2 5 2 2 1 4 3 1 5 4 2 1 5 7 1 7 5 2 1 4 1 1 2 5 3 2 6 4 1 2 3 3 4 5 */

// 1 5 7 1 7 5