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

using namespace std;

typedef long long ll;

struct Tea
{
  ll v, t, e;
};

bool operator<(const Tea &lhs, const Tea &rhs)
{
  return lhs.t > rhs.t;
}

void solve()
{
  int N;
  cin >> N;
  vector<Tea> current(N);
  vector<Tea> desired(N);

  ll sum1, sum2;
  sum1 = sum2 = 0;

  for (int i = 0; i < N; i++)
  {
    int V, A, B;
    cin >> V >> A >> B;
    sum1 += A * V;
    sum2 += B * V;
    current[i].v = desired[i].v = V;

    current[i].t = A;
    desired[i].t = B;

    current[i].e = V * A;
    desired[i].e = V * B;
  }

  if (sum2 != sum1)
  {
    cout << "NIE\n";
    return;
  }

  sort(current.begin(), current.end());
  sort(desired.begin(), desired.end());

  ll c1, c2;
  ll prev = 0;
  c1 = c2 = 0;

  ll d1 = 0;
  ll d2 = 0;

  bool ok = true;

  int j = 0;
  for(int i = 0; i < N; i++)
  {
    c2 += desired[i].e;
    d2 += desired[i].v;
    while (c1 < c2)
    {
      prev = d1;
      d1 += current[j].v;
      c1 += current[j].e;
      j++;
    }

    ll y = c1 - current[j - 1].e + (d2 - prev) * current[j - 1].t;
    if (y < c2)
    {
      ok = false;
      break;
    }
  }



  cout << (ok ? "TAK" : "NIE") << "\n";
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int Z;
  cin >> Z;
  while (Z--)
    solve();
}