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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <string>
#include <set>
#include <cstring>
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;

#define PB push_back
#define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i,n) for(int i=0,_=(n);i<_;++i)
#define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1e9 + 9;

struct Rectangle {
  static int w;
  int x1, y1, x2, y2, id;

  bool operator<(const Rectangle &other) const {
    return x1 < other.x1;
  }

  bool operator ==(const Rectangle &other) const {
    return x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2;
  }

  int height() const {
    return y2 - y1;
  }

  bool collide(const Rectangle &other) const {
    return (height() + other.height()) > w;
  }
};

int Rectangle::w;

void inline jeden() {
  int n, w;
  scanf("%d%d", &n, &w);
  Rectangle::w = w;
  vector<Rectangle> rect[2];
  vi left(n, -1), right(n, -1);

  REP(k, 2) {

    REP(i, n) {
      int x1, y1, x2, y2;
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      if (x1 > x2) {
        swap(x1, x2);
      }
      if (y1 > y2) {
        swap(y1, y2);
      }

      rect[k].PB((Rectangle) {
        x1, y1, x2, y2, i
      });
    }
  }
  vector<Rectangle> &r = rect[0];

  REP(k, 2) {
    sort(rect[k].begin(), rect[k].end());
  }

//  REP(i, n) {
  //    printf("%d id=%d h=%d\n", i, r[i].id, r[i].height());
  //  }
  priority_queue<pii> q;

  REP(i, n) {

    while (!q.empty()) {
      pii qtop = q.top();
      int j = qtop.second;
      if (r[i].collide(r[j])) {
        right[r[j].id] = r[i].id;
        q.pop();
      } else {
        break;
      }
    }
    q.push(pii(r[i].height(), i));
  }
  q = priority_queue<pii>();

  FORD(i, n - 1, 0) {
    while (!q.empty()) {
      pii qtop = q.top();
      int j = qtop.second;
      if (r[i].collide(r[j])) {
        left[r[j].id] = r[i].id;
        q.pop();
      } else {
        break;
      }
    }
    q.push(pii(r[i].height(), i));
  }

  map<int, int> where;

  REP(i, n) {
    where[rect[1][i].id] = i;
  }
  bool ok = true;

  REP(i, n) {
    if (left[i] != -1) {
      if (!(where[left[i]] < where[i])) {
        ok = false;
        break;
      }
    }
    if (right[i] != -1) {
      if (!(where[right[i]] > where[i])) {
        ok = false;
        break;
      }
    }
  }

  //  REP(i, n) {
  //    printf("%d: %d %d\n", i, left[i], right[i]);
  //  }
  puts(ok ? "TAK" : "NIE");
}

int main() {
  int z;
  scanf("%d", &z);
  while (z--)
    jeden();
}