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
#include <assert.h>
#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>

using namespace std;

struct S {
  int v;
  mutable int cnt;
  S(int v, int cnt) : v(v), cnt(cnt) {}
};

bool operator<(const S& l, const S& r) {
  return l.v < r.v;
}

set<S>::iterator insert(set<S>& s, S x) {
  auto it = s.insert(x);
  if (!it.second) {
    it.first->cnt += x.cnt;
  }
  return it.first;
}

bool step(set<S>& x, set<S>& y) {
  auto x2 = --x.end();
  auto z = --y.end();

  if (z->v > x2->v) {
    return false;
  } else if (z->v == x2->v) {
    int delta = min(z->cnt, x2->cnt);
    z->cnt -= delta;
    x2->cnt -= delta;
  } else {
    auto x1 = --x.upper_bound(*z);
    if (x1 == x2) {
      return false;
    } else if (z->v == x1->v) {
      int delta = min(z->cnt, x1->cnt);
      z->cnt -= delta;
      x1->cnt -= delta;
    } else if (z->cnt == 1) {
      z->cnt -= 1;
      x1->cnt -= 1;
      x2->cnt -= 1;
      insert(x, S(x1->v + x2->v - z->v, 1));
    } else {
      int d2 = x2->v - z->v;
      int d1 = z->v - x1->v;

      if (d1 % d2 == 0) {
        int long_step = (d1 / d2);

        int full_long_steps =
            min(min(x2->cnt / (long_step), z->cnt / (long_step + 1)), x1->cnt);

        if (full_long_steps > 0) {
          x2->cnt -= full_long_steps * (long_step);
          z->cnt -= full_long_steps * (long_step + 1);
          x1->cnt -= full_long_steps;
        } else {
          int partial_step = min(min(z->cnt, x2->cnt), long_step);
          x2->cnt -= partial_step;
          z->cnt -= partial_step;
          x1->cnt -= 1;
          insert(x, S(x1->v + d2 * partial_step, 1));
        }

      } else {
        int long_step = (d1 / d2) + 1;

        int full_long_steps =
            min(min(x2->cnt / (long_step), z->cnt / (long_step)), x1->cnt);

        if (full_long_steps > 0) {
          x2->cnt -= full_long_steps * (long_step);
          z->cnt -= full_long_steps * (long_step);
          x1->cnt -= full_long_steps;
          insert(x, S(x1->v + long_step * d2, full_long_steps));

        } else {
          int partial_step = min(min(z->cnt, x2->cnt), long_step);
          x2->cnt -= partial_step;
          z->cnt -= partial_step;
          x1->cnt -= 1;
          insert(x, S(x1->v + d2 * partial_step, 1));
        }
      }
    }
    if (x1->cnt == 0) {
      x.erase(x1);
    }
  }

  if (z->cnt == 0) {
    y.erase(z);
  }

  if (x2->cnt == 0) {
    x.erase(x2);
  }
  return true;
}

bool solve() {
  int n;
  scanf("%d", &n);
  set<S> x, y;
  for (int i = 0; i < n; i++) {
    int l, a, b;
    scanf("%d %d %d", &l, &a, &b);
    insert(x, S(a, l));
    insert(y, S(b, l));
  }
  while (!x.empty() && !y.empty()) {
    if (!step(x, y)) {
      return false;
    }
  }
  return x.empty() && y.empty();
}

int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    printf("%s\n", solve() ? "TAK" : "NIE");
  }
}