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
/*
  Zadnie: Herbata [B] (her)
  Autor: Adam Szeruda
  Potyczki Algorytmiczne 2019
*/

#include <iostream>
using namespace std;

#define DBG(x)
#ifndef DBG
#define DBG(x) cerr << #x << " = " << x << '\n'
#endif

#define FOR(i, b, e) for (int i = (b); i < (int)(e); i++)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef unsigned long long ull;

int l[100009];
int a[100009];
int b[100009];

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t, n;
  ull QM,  // Q * M
      QpM, // Q' * M
      E, M;

  double Q, SM, SpM;

  cin >> t;
  while (t--)
  {
    M = QM = QpM = SM = SpM = 0;

    cin >> n;
    REP(i, n)
    {
      cin >> l[i] >> a[i] >> b[i];

      M += l[i];
      QM += l[i] * b[i];
      QpM += l[i] * a[i];
    }

    Q = (double)QM / M;

    DBG(M);
    DBG(QM);
    DBG(QpM);
    DBG(Q);

    REP(i, n)
    {
      SM += abs(b[i] - Q) * l[i];
      SpM += abs(a[i] - Q) * l[i];
    }

    DBG(SM);
    DBG(SpM);

    if (QM != QpM || SM > SpM)
    {
      cout << "NIE\n";
      continue;
    }

    cout << "TAK\n";
  }
}