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
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

typedef vector<pair<pair<int, int>, int> > Vector;

class Tree {
 public:
  Tree(const Vector &v) {
    base_ = 2;
    while (base_ < v.size()) base_ <<= 1;
    v_.resize(2 * base_, 0);
    for (int i = 0; i < v.size(); ++i) v_[base_ + i] = v[i].first.second;
    for (int i = base_ - 1; i > 0; --i) v_[i] = max(v_[2 * i], v_[2 * i + 1]);
  };

  int Get(int i) {
    int ret = 0;
    i += base_;
    while (i > 1) {
      if (i & 1) ret = max(ret, v_[--i]);
      i /= 2;
    }
    return ret;
  }

  void Set(int i, const int v) {
    i += base_;
    v_[i] = v;
    while (i > 1) {
      i /= 2;
      v_[i] = max(v_[2 * i], v_[2 * i + 1]);
    }
  }

 private:
  int base_;
  vector<int> v_;
};

int n;

void Read(Vector *v) {
  for (int i = 0; i < n; ++i) {
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    v->push_back(make_pair(make_pair(min(x1, x2), max(y1 - y2, y2 - y1)), i));
  }
  sort(v->begin(), v->end());
}

void BuildIndex(const Vector &v, vector<int> *ind) {
  ind->resize(n);
  for (int i = 0; i < n; ++i) (*ind)[v[i].second] = i;
}

void Solve() {
  int w;
  scanf("%d%d", &n, &w);
  Vector before, after;
  Read(&before);
  Read(&after);
  vector<int> index;
  BuildIndex(before, &index);
  Tree tree(before);

  for (int i = 0; i < n; ++i) {
    const int car_number = after[i].second;
    const int car_height = after[i].first.second;
    const int car_position = index[car_number];
    const int obstacle_height = tree.Get(car_position);
    tree.Set(car_position, 0);
    if (car_height + obstacle_height > w) {
      printf("NIE\n");
      return;
    }
  }

  printf("TAK\n");
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) Solve();
  return 0;
}