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
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <strings.h>

using namespace std;
int n, Z, w;

class R {
	public:
	int x1, x2, y1, y2, id;
	R(int _x1, int _x2, int _y1, int _y2, int _id): x1(_x1), x2(_x2), y1(_y1), y2(_y2) , id(_id){}
};
class Cmp {
	public:
	bool operator()(const R&f, const R&s)
	{
		if (f.x1 < s.x1) return true;
		if (f.x1==s.x1 && f.x2 < s.x2) return true;
		return false;
	}
};


vector<R> p, k;// p-oczatek, k-oniec  ustawienia
set  <int> X;	// zbior x
map<int, int> Xmap; // wezel w drzewie. skalowanie x

inline int max(int a, int b)
{
	return a < b  ? b : a;
}
#ifdef D
const int M = 4;
#else
const int M = 32768;
#endif

int t[2*M];

//drzewo
void initDrzewo()
{
	bzero(t, 2 * M * sizeof(int));
}
int query(int pocz)
{
	int va = M + pocz;
	int vb = 2*M - 1;
	int lmaks = t[va];
	int rmaks = t[vb];
	while (va  >= 1)
	{
		va /=2; vb /=2;
		if (va < vb -1)
		{
			if (va % 2 == 0)
				lmaks = max(lmaks, t[va+1]);
			if (vb % 2 == 1)
				rmaks = max(rmaks, t[vb -1]);
		}
		
	}
	return max(lmaks, rmaks);
}
void insert(int p, int h)
{
	int va = M+p;
	t[va] = t[va] < h ? h : t[va];
	while (va != 1)
	{
		va /=2;
		t[va] = max(t[va*2], t[va*2 + 1]);
	}
}

//---------------------------------------------------------
int main()
{
	scanf("%d", &Z);
	for (int z = 0; z < Z; z++)
	{
		int x1, x2, y1, y2;
		X.clear();
		Xmap.clear();
		initDrzewo();
		
		
		scanf("%d %d", &n, &w);
		for (int i = 0; i < n; i++)
		{
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			R r(x1, x2, y1, y2, i);
			p.push_back(r);
		}
		// k-oncowe ustawienie klockow		
		for (int i = 0; i < n; i++)
		{
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			R r(x1, x2, y1, y2, i);
			k.push_back(r);
			X.insert(x1);
			X.insert(x2);
		}
        
		int pos = 0;
		// skalowanie
		for (set<int>::iterator it = X.begin(); it != X.end(); ++it, pos++)
		{
			Xmap[*it] = pos;
		}
		sort(p.begin(), p.end(), Cmp());

		
		bool OK = true;
		for (int i = 0; i < n; i++)
		{
			int h = p[i].y2 - p[i].y1;
			// p[p[i].id].x1 ma by. przeniesione do 
			// do drzewa na Xmap[k[p[i].id].x1]
			
			if (query(Xmap[k[p[i].id].x1] + 1) + h > w)
			{
				OK = false;
				break;
			}
			insert(Xmap[k[p[i].id].x1 ], h);
			insert(Xmap[k[p[i].id].x2 ], h);
		}
		
		if (OK) 
			printf ("TAK\n");
		else 
			printf ("NIE\n");
	}
	return 0;
}