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
// Lupus Nocawy 15 V 2014, PA2014
// http://potyczki.mimuw.edu.pl/
// Runda 3
// Zadanie: PAR
// Parking [B]

#include <cstdio>
#include <iostream>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define REP(i,n) for(int i=0, _n=n; i<_n; ++i)
#define FOR(i,a,b) for(int i=(a), _b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a), _b=(b); i>=_b; --i)
#define IT(i,x) __typeof__(x) i=x
#define FOREACH(it,x) for(__typeof__((x).begin()) it=(x).begin(); it!=(x).end(); ++it)
#define ALL(x) x.begin(),x.end()
#define MP make_pair
#define PB push_back
#define DEBUG(x) if(debug) cout << x << endl;
typedef long long int lli;
typedef unsigned long long int llu;
typedef pair<int,int> pii;
const int debug=0;

const int INF = 2147483647;
const int max_n = 50000;
const int max_w = 1000000000;

class car{
public:
	int i, x, h;
	car(int i, int x, int h): i(i), x(x), h(h) {}
};

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

class IntervalTree{
public:
	int size;
	IntervalTree(int s){
		size = s;
		half=1;
		while(half < size){
			half *= 2;
		}
		container_size = half*2;
		T.resize(container_size,0);
	}
	void put(int i, int val){
		T[half+i] = val;
	}
	void updateAll(void){
		for(int i=half-1; i>=1; i--){
			T[i] = max(T[left(i)], T[right(i)]);
		}
	}
	void update(int i, int val){
		i += half;
		T[i] = val;
		while(i>1){
			i=parent(i);
			T[i] = max(T[left(i)], T[right(i)]);
		}
	}
	int get(int s, int e){	// inclusive [s,e]
		//assert(s<=e);
		if(s>e)
			return 0;
		s += half;
		e += half;
		int r = max(T[s],T[e]);
		s = parent(s+1);
		e = parent(e-1);
		while(s<=e){
			r = max(r,max(T[s],T[e]));
			s = parent(s+1);
			e = parent(e-1);
		}
		return r;
	}
	void print(){
		for(int s=1; s<=half; s*=2){
			FOR(i,s,2*s-1){printf("%d ", T[i]);}
			printf("\n");
		}
	}
private:
	int container_size, half;
	vector<int> T;
	inline int parent(int i){ return i/2; }
	inline int left(int i)  { return i*2; }
	inline int right(int i) { return i*2+1; }
};

void solve(void){
	int n, w;
	scanf("%d %d ", &n, &w);
	vector<car> S, E;
	REP(i,n){
		int x1,y1,x2,y2;
		scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);
		S.PB(car(i, min(x1,x2), abs(y2-y1)));
	}
	REP(i,n){
		int x1,y1,x2,y2;
		scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);
		E.PB(car(i, min(x1,x2), abs(y2-y1)));
	}
	
	sort(ALL(S));
	sort(ALL(E));
	vector<int> posS(n);
	REP(i,n){
		posS[S[i].i]=i;	// lol @me... 
	}
	
	IntervalTree T(n);
	REP(i,n){
		T.put(i, S[i].h);
	}
	T.updateAll();
	
	REP(i,n){
		int maxH = T.get(0, posS[E[i].i]-1);
		if(maxH + E[i].h > w){	// nie da sie przecisnac autka na poczatek
			printf("NIE\n");
			return;
		}
		else{
			T.update(posS[E[i].i], 0);
		}
	}
	printf("TAK\n");
	return;
}

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