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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>

#define SC(n) scanf("%d", &n)
#define SC2(n, m) scanf("%d %d", &n, &m)
#define SCC(c) scanf(" %c", &c)
#define SCS(b) scanf("%s", b)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FORE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i)
#define PR(n) printf("%d ", (int) (n)) 
#define PRN(n) printf("%d\n", (int) (n)) 
#define elif else if
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(), v.end()
#define itr iterator
#define prd if(0) printf

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int, pi> pii;
typedef vector<pii> vpii;
typedef vector<vpi> vvpi;
typedef map<int, vpi> mvpi;

const int NMAX = 50*1000 + 7, TMAX = 300 * 1000, PMAX = (1<<16);

int tn, n, w, x1, x2, yy1, yy2, shit, car, n1, n2;
int h[NMAX], rev2[NMAX];
pi p1[NMAX], p2[NMAX];
int mag, t[TMAX], d[TMAX];

void init() {
	mag = 1;
	while(mag < n)
		mag *= 2;
	
	REP(i, n)
		t[mag + i] = h[p1[i].yy];
	FORE(i, mag + n, 2*mag-1)
		t[i] = 0;
	FORD(i, mag - 1, 1)
		t[i] = max(t[2*i], t[2*i+1]);
}

int findLast(int l, int r, int hh) { // ostatni z przedzialu [a,b] o wysokosci > hh
	l += mag;
	r += mag;
	int cand = -1;
	
	while(l < r) {
	    if(l % 2 == 1) {
	      if(t[l] > hh) if(cand == -1 || d[cand] < d[l])
	      	cand = l;
	      l = l/2 + 1;
	    }
	    else
	      l /= 2;
	    
	    if(r % 2 == 0) {
	      if(t[r] > hh) if(cand == -1 || d[cand] < d[r])
	      	cand = r;
	      r = r/2 - 1;
	    }
	    else
	      r /= 2;
	  }
  	if(l == r && t[l] > hh) if(cand == -1 || d[cand] < d[l])
		cand = l;
		
	if(cand == -1)
		return -1;
	while(cand < mag) {
		if(t[2*cand + 1] > hh)
			cand = 2*cand + 1;
		else
			cand *= 2;
	}
	return p1[cand - mag].yy;
}

int findFirst(int l, int r, int hh) {
	l += mag;
	r += mag;
	int cand = -1;
	
	while(l < r) {
	    if(l % 2 == 1) {
	      if(t[l] > hh) if(cand == -1 || d[cand] > d[l])
	      	cand = l;
	      l = l/2 + 1;
	    }
	    else
	      l /= 2;
	    
	    if(r % 2 == 0) {
	      if(t[r] > hh) if(cand == -1 || d[cand] > d[r])
	      	cand = r;
	      r = r/2 - 1;
	    }
	    else
	      r /= 2;
	  }
  	if(l == r && t[l] > hh) if(cand == -1 || d[cand] > d[l])
	      	cand = l;
		
	if(cand == -1)
		return n;
	while(cand < mag) {
		if(t[2*cand] > hh)
			cand *= 2;
		else
			cand = 2*cand + 1;
	}
	return p1[cand - mag].yy;
}
	

int main() {
	FORE(i, PMAX, 2*PMAX - 1)
		d[i] = i;
	FORD(i, PMAX - 1, 1)
		d[i] = d[2*i];
	
	SC(tn);
	REP(ti, tn) {
		SC2(n, w);
		shit = 0;
		
		REP(i, n) {
			SC2(x1, yy1);
			SC2(x2, yy2);
			p1[i] = mp(min(x1, x2), i);
			h[i] = abs(yy2 - yy1);
		}
		REP(i, n) {
			SC2(x1, yy2);
			SC2(x2, yy2);
			p2[i] = mp(min(x1, x2), i);
		}
		sort(p1, p1+n);
		sort(p2, p2+n);
		
		REP(i, n)
			rev2[p2[i].yy] = i;
		
		init();
		REP(i, n) {
			car = p1[i].yy;		
			n1 = findLast(0, i-1, w - h[car]);
			n2 = findFirst(i+1, n-1, w - h[car]);
			prd("%d (%d) : %d %d\n", car, h[car], n1, n2);
			
			if(n1 != -1 && rev2[n1] > rev2[car]) {
				prd("collision %d %d\n", car, n1);
				shit = 1;
				break;
			}
			if(n2 != n && rev2[n2] < rev2[car]) {
				prd("collision %d %d\n", car, n2);
				shit = 1;
				break;
			}
		}
		if(shit)
			printf("NIE\n");
		else
			printf("TAK\n");
				
	}
	return 0;
}