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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <algorithm>
#include <cassert>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)

template<class T> inline int size(const T&c) { return c.size(); }

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  unsigned x = Get<unsigned>();
  return static_cast<int>(x);
}

Input IN;


template<class T>
class Fenwick {
 public:
  explicit Fenwick(int n): tab(n+1) {}
  void Add(int p, const T &v);
  T Sum(int n) const;
 private:
  vector<T> tab;
};

template<class T>
void Fenwick<T>::Add(int p, const T &v) {
  ++p;
  while(p < static_cast<int>(tab.size())) {
    tab[p] += v;
    p += (p & -p);
  }
}

template<class T>
T Fenwick<T>::Sum(int n) const {
  T res = T();
  while(n) {
    res += tab[n];
    n &= (n-1);
  }
  return res;
}


struct CarInfo {
  int height;
  int width;
  int x[2];
} __attribute__((aligned(16)));

struct Car {
  int index;
  int height;
  int x;
  int bigleft;
} __attribute__((aligned(16)));

const int MAX_CARS = 50000;

int NCARS;
int HEIGHT;
CarInfo CAR_INFOS[MAX_CARS];
Car CARS[MAX_CARS];
vector<int> ALL_HEIGHTS;

inline bool operator<(const Car &a, const Car &b) {
  return a.x < b.x;
}

bool ReadInput() {
  IN(NCARS, HEIGHT);
  bool ok = true;
  REP(iter,2) REP(i,NCARS) {
    int x1,y1,x2,y2;
    IN(x1,y1,x2,y2);
    if(x1>x2) swap(x1,x2);
    if(y1>y2) swap(y1,y2);
    CarInfo &ci = CAR_INFOS[i];
    ci.x[iter] = x1;
    if(iter==0) {
      ci.height = y2-y1;
      ci.width = x2-x1;
    } else {
      if(ci.height != y2-y1) ok = false;
      if(ci.width != x2-x1) ok = false;
    }
  }
  return ok;
}

void CalcAllHeights() {
  ALL_HEIGHTS.clear(); ALL_HEIGHTS.reserve(NCARS);
  REP(i,NCARS) {
    ALL_HEIGHTS.push_back(CAR_INFOS[i].height);
  }
  sort(ALL_HEIGHTS.begin(), ALL_HEIGHTS.end());
  ALL_HEIGHTS.erase(unique(ALL_HEIGHTS.begin(), ALL_HEIGHTS.end()), ALL_HEIGHTS.end());
}

template<int iter>
bool Process() {
  if(iter==1) {
    REP(i,NCARS) {
      Car &car = CARS[i];
      car.x = CAR_INFOS[car.index].x[1];
    }
  }
  sort(CARS, CARS+NCARS);
  Fenwick<int> fenwick(size(ALL_HEIGHTS));
  REP(i,NCARS) {
    Car &car = CARS[i];
    int firstBig = lower_bound(ALL_HEIGHTS.begin(), ALL_HEIGHTS.end(), HEIGHT - car.height + 1) - ALL_HEIGHTS.begin();
    int bigleft = i - fenwick.Sum(firstBig);
    if(iter==0) {
      car.bigleft = bigleft;
    } else {
      if(car.bigleft != bigleft) return false;
    }
    int myHeight = lower_bound(ALL_HEIGHTS.begin(), ALL_HEIGHTS.end(), car.height) - ALL_HEIGHTS.begin();
    fenwick.Add(myHeight, 1);
  }
  return true;
}

bool Solve() {
  CalcAllHeights();
  REP(i,NCARS) {
    Car &car = CARS[i];
    const CarInfo &ci = CAR_INFOS[i];
    car.index = i;
    car.height = ci.height;
    car.x = ci.x[0];
    car.bigleft = -1;
  }
  Process<0>();
  return Process<1>();
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr);

  int ntc; IN(ntc);
  REP(tc,ntc) {
    bool ok = ReadInput();
    ok = ok && Solve();
    cout << (ok?"TAK\n":"NIE\n");
  }
}