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

using namespace std;

struct mug{
  long long t;
  long long c;
  const bool operator<(const mug& rhs) const{
    return t < rhs.t;
  }
};

vector <mug> A;
vector <mug> B;
int n;

void obt(){
  A.clear();
  B.clear();
  for(int a = 0; a < n; a++){
    mug temp;
    cin >> temp.c >> temp.t;
    A.push_back(temp);
    cin >> temp.t;
    B.push_back(temp);
  }
  sort(A.begin(),A.end());
  sort(B.begin(),B.end());
}

bool sol(){
  int a_it = n-1;
  int b_it = n-1;
  long long sum = 0;
  while(true){
    sum += min(A[a_it].c,B[b_it].c)*(A[a_it].t - B[b_it].t);
    if(sum < 0)
      return false;
    if(a_it == 0 && b_it == 0)
      break;
    if(A[a_it].c > B[b_it].c){
      A[a_it].c -= B[b_it].c;
      B[b_it].c = 0;
      if(b_it != 0)
        b_it--;
    }
    else{
      if(A[a_it].c < B[b_it].c){
        B[b_it].c -= A[a_it].c;
        A[a_it].c = 0;
        if(a_it != 0)
          a_it--;
      }
      else{
        if(a_it != 0)
          a_it--;
        if(b_it != 0)
          b_it--;
      }
    }
  }
  if(sum != 0)
    return false;
  return true;
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin >> t;
  while(t--){
    cin >> n;
    obt();
    if(sol())
      cout << "TAK" << "\n";
    else
      cout << "NIE" << "\n";
  }
}