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

typedef long long i64;
map<i64, i64> targets;
map<i64, i64> liquids;
i64 excessEnergy;

bool step()
{
  pair<i64, i64> nextTarget = *targets.rbegin();
  pair<i64, i64> nextLiquid = *liquids.rbegin();
  i64 massDelta = min(nextTarget.second, nextLiquid.second);
  i64 energyDelta = massDelta * (nextTarget.first - nextLiquid.first);
  targets[nextTarget.first] -= massDelta;
  if (targets[nextTarget.first] == 0)
  {
    targets.erase(nextTarget.first);
  }
  liquids[nextLiquid.first] -= massDelta;
  if (liquids[nextLiquid.first] == 0)
  {
    liquids.erase(nextLiquid.first);
  }
  excessEnergy -= energyDelta;
  return excessEnergy >= 0;
}

bool solve()
{
  while (!targets.empty())
  {
    if (!step())
    {
      return false;
    }
  }
  return excessEnergy == 0;
}

void read()
{
  int n;
  scanf("%d", &n);
  while (n--)
  {
    i64 l, a, b;
    scanf("%lld%lld%lld", &l, &a, &b);
    targets[b] += l;
    liquids[a] += l;
  }
}
void clear()
{
  excessEnergy = 0;
  liquids.clear();
  targets.clear();
}

int main()
{
  int t;
  scanf("%d", &t);
  while (t--)
  {
    clear();
    read();
    if (solve())
    {
      printf("TAK\n");
    }
    else
    {
      printf("NIE\n");
    }
  }
  return 0;
}