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
#include <cstdio>
#include <algorithm>
#define MP make_pair
#define ST first
#define ND second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;

const int N = 5e4;
const int S = 1<<16;

int n, h, at[N+1];
PIII a[N+1], b[N+1];

struct tree
{
	int t[2*S];
	
	void set(int p, int v)
	{
		t[p += S] = v;
		while (p /= 2) t[p] = max(t[2*p], t[2*p+1]);
	};
	
	int get(int a, int b)
	{
		int r = 0;
		a += S-1; b += S+1;
		while (a+1 < b) {
			if (a%2==0) r = max(r, t[a+1]);
			if (b%2==1) r = max(r, t[b-1]);
			a /= 2; b /= 2;
		}
		return r;
	}
} t;

void read(PIII t[])
{
	for (int i=1; i<=n; ++i) {
		int x1, y1, x2, y2;
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		t[i] = MP(MP(max(x1, x2), abs(y1-y2)), i);
	}
	sort(t+1, t+1+n);
}

int main()
{
	int q; scanf("%d", &q);
	while (q--) {
		scanf("%d%d", &n, &h);
		read(a); read(b);
		
		for (int i=1; i<=n; ++i) {
			t.set(i, a[i].ST.ND);
			at[a[i].ND] = i;
		}
		
		bool ok = true;
		for (int i=1; i<=n; ++i) {
			int p = at[b[i].ND];
			ok &= t.get(1, p-1) <= h - a[p].ST.ND;
			t.set(p, 0);
		}
		
		puts(ok ? "TAK" : "NIE");
	}
	
	return 0;
}