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
#include <cstdio>
#include <algorithm>
using namespace std;

int nr[50000];
int h[50000];
pair<int, int> p[50000];

const int S = 1 << 16;
int mx[S << 1];

void clear(int x)
{
    x += S;
    mx[x] = 0;
    while(x > 1)
    {
	x >>= 1;
	mx[x] = max(mx[x * 2], mx[x * 2 + 1]);
    }
}

int max(int b)
{
    b += S;
    int a = S, ans = 0;
    while(a < b)
    {
	if((b & 1) == 0) ans = max(ans, mx[b--]);
	a >>= 1;
	b >>= 1;
    }
    if(a == b) ans = max(ans, mx[a]);
    return ans;
}

int main()
{
    int test, n, w, x1, x2, y1, y2;
    scanf("%d", &test);
    while(test--)
    {
	scanf("%d %d", &n, &w);
	for(int i = 0; i < n; i++)
	{
	    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
	    p[i] = make_pair(min(x1, x2), i);
	    h[i] = abs(y1 - y2);
	}
	sort(p, p + n);
	for(int i = 0; i < n; i++)
	    nr[p[i].second] = i;
	for(int i = 0; i < n; i++)
	    mx[nr[i] + S] = h[i];
	for(int i = n; i < S; i++)
	    mx[i + S] = 0;
	for(int i = S - 1; i > 0; i--)
	    mx[i] = max(mx[i*2], mx[i*2+1]);
	for(int i = 0; i < n; i++)
	{
	    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
	    p[i] = make_pair(min(x1, x2), i);
	}
	sort(p, p + n);
	bool ok = true;
	for(int i = 0; i < n; i++)
	{
	    int k = p[i].second;
	    if(h[k] + max(nr[k] - 1) > w)
	    {
		ok = false;
		break;
	    }
	    clear(nr[k]);
	}
	puts(ok ? "TAK" : "NIE");
    }
}