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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define FOR(i,n) for(int i=0;i<n;++i)
#define FORB(i,b,n) for(int i=b;i<n;++i)
#define REV(i,n) for(int i=n;i>=0;--i)
#define FOREACH(T, it, v) for(T::iterator it = v.begin(); it !=v.end(); ++it)
#define print(STRING,INT) printf("%s= %d\n", STRING, INT)
#define scan(INT) scanf("%d", &INT)


typedef unsigned int u32;
const int INF = (0u - 11)/2;

typedef long long ll;
typedef set<int> SI;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef set<long long> SL;
typedef map<int, int> MII;
typedef multimap<int, int> MMII;


int DEBUG_ = 1;



void log(char* S, long long a)
{
    if(DEBUG_)
    {
        printf("%s= %I64d\n", S, a);
    }
}


struct A
{
	int w;
	int x1;
	int x2;
	int index;
//	A(int _a, int _b, int _index) : w(_a), x(_b), index(_index) {}
	A() {}
};

struct B
{
	int a;
	int index;
	B() {};
};

bool compare(B a, B b)
{
	if(a.a != b.a) return a.a < b.a;
	return a.index < b.index;
}

bool rev_compare(B a, B b)
{
	if(a.a != b.a) return a.a > b.a;
	return a.index > b.index;
}

int solve()
{
	int n, maxW;
	scan(n);
	scan(maxW);
	int a, b, c, d;
	vector<A> F(n, A());
	vector<B> R(n, B());
	FOR(i,n)
	{
		scan(a); scan(b); scan(c); scan(d);
		F[i].index = i;
		F[i].w = abs(d-b);
		F[i].x1 = min(a,c);
	}
	FOR(i,n)
	{
		scan(a); scan(b); scan(c); scan(d);
		F[i].x2 = min(a,c);
		R[i].a = F[i].w;
		R[i].index = F[i].index;

	}
	sort(R.begin(), R.end(), rev_compare);
	//FOR(i,n)
	//{
	//	cout << R[i].a << " " << R[i].index << endl;
	//}



	MII PERM_A;
	MII PERM_B;
	PERM_A[-1] = -1; 
	PERM_A[INF] = -1;
	PERM_B[-1] = -1; 
	PERM_B[INF] = -1;

	int toCheck = R[0].index;
	PERM_A[F[toCheck].x1] = F[toCheck].index;
	PERM_B[F[toCheck].x2] = F[toCheck].index;

	int biggestAdded = 0;
	int biggestAddedIndex = toCheck;
	MII::iterator iterA;
	MII::iterator iterB;
//	cout << "boss dodany " << endl;
	FORB(i,1,n)
	{
	////	cout << "dodaje i " << i << " " << R[i].index << endl;
		toCheck = R[i].index;

		//cout <<F[toCheck].w << " " << F[biggestAddedIndex].w << endl;
		while(F[toCheck].w + F[biggestAddedIndex].w <= maxW)
		{
		//	cout << " juz mozna zaczac usuwac " << i << endl;
			if(biggestAdded == 0) return true;

			PERM_A.erase(PERM_A.find(F[biggestAddedIndex].x1));
			PERM_B.erase(PERM_B.find(F[biggestAddedIndex].x2));
			biggestAddedIndex = R[--biggestAdded].index;
		}
	//	cout << " dodaj " << F[toCheck].index << " "<< F[toCheck].x1 << endl;
		PERM_A[F[toCheck].x1] = F[toCheck].index;
		PERM_B[F[toCheck].x2] = F[toCheck].index;

		//FOREACH(MII, it, PERM_A)
		//{
		//	cout << it->first << " ";
		//}
		//cout << endl;
		//FOREACH(MII, it, PERM_A)
		//{
		//	cout << it->second << " ";
		//}
		//cout << endl;
		//FOREACH(MII, it, PERM_B)
		//{
		//	cout << it->first << " ";
		//}
		//cout << endl;
		//FOREACH(MII, it, PERM_B)
		//{
		//	cout << it->second << " ";
		//}

		//cout << endl;
		iterA = PERM_A.find(F[toCheck].x1);
		iterB = PERM_B.find(F[toCheck].x2);
	//	cout << iterA->second << endl;
		if((--iterA)->second != (--iterB)->second) return false; // lewy
	//	cout << iterA->second << endl;
		++iterA; ++iterB; // powrot
		//cout << iterA->second << endl;
		if((++iterA)->second != (++iterB)->second) return false;// prawy
	//	cout << iterA->second << endl;
		--iterA; --iterB;
		//cout << iterA->second << endl;

		if(biggestAdded == i - 1)
		{
			biggestAdded++;
			biggestAddedIndex = toCheck;
		}
		else
		{
	//		cout << "wyjazd dodani ostatnio " << endl;
				PERM_A.erase(iterA);
				PERM_B.erase(iterB);
		}

	}
	return true;
}


int main()
{	
	int T;
	scan(T);
	while(T--)
	{
	//	cout << T << endl;
		if(solve())
		{
			cout << "TAK" << endl;
		}
		else
		{
			cout << "NIE" << endl;
		}
	}

	return 0;
}