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

typedef vector <int > VI;
typedef vector < VI > VVI;
typedef long long LL;
typedef vector < LL > VLL;
typedef vector < double > VD;
typedef vector < string > VS;
typedef pair<int,int> PII;
typedef vector <PII> VPII;
typedef istringstream ISS;

#define ALL(x) x.begin(),x.end()
#define REP(i,n) for (int i=0; i<(n); ++i)
#define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var)
#define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var)
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it)
#define PB push_back
#define PF push_front
#define MP(a,b) make_pair(a,b)
#define ST first
#define ND second
#define SIZE(x) (int)x.size()

const int BASE = 64 * 1024;
int drzewo[2 * BASE];
const int N = 51000;
int position[N][4];
int position2[N][4];

void drzewo_ustaw(int pos, int h) {
  pos += BASE;
  while (pos >= 1) {
    drzewo[pos] = max(drzewo[pos], h);
    pos >>= 1;
  }
}

int drzewo_max(int a, int b) {
  int res = 0;
  a += BASE; b += BASE;
  while (a < b) {
    if (a & 1) res = max(res, drzewo[a++]);
    if (!(b & 1)) res = max(res, drzewo[b--]);
    a >>= 1;
    b >>= 1;
  }
  if (a == b) res = max(res, drzewo[a]);
  return res;
}

int numer_docelowy[N];

int main(){
  int testy;
  scanf("%d",&testy);
  while (testy--){
    memset(drzewo, 0, sizeof(drzewo));
    int n, w;
    scanf("%d %d", &n, &w);
    REP(i,n){
      REP(j,4) scanf("%d", position[i]+j);
      REP(j,2) if (position[i][j] > position[i][j+2]) swap(position[i][j], position[i][j+2]);
    }

    REP(i,n){
      REP(j,4) scanf("%d", position2[i]+j);
      REP(j,2) if (position2[i][j] > position2[i][j+2]) swap(position2[i][j], position2[i][j+2]);
    }

    VPII v;
    v.reserve(n);
    REP(i,n) v.PB(MP(position2[i][0], i));
    sort(ALL(v));
    REP(i,n) numer_docelowy[v[i].ND] = i;

    v.clear();
    REP(i,n) v.PB(MP(position[i][0], i));
    sort(ALL(v));
    int ok = 1;
    REP(i,n) {
      int h = position[v[i].ND][3] - position[v[i].ND][1];
      int x = numer_docelowy[v[i].ND];
      //printf("x=%d h=%d\n", x, h);
      if (drzewo_max(x,n-1) + h > w) {
        ok = 0;
        break;
      }
      drzewo_ustaw(x,h);
    }
    puts(ok ? "TAK" : "NIE");
  }
  return 0;
}