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
#include <cstdio>
#include <algorithm>
#include <set>

using namespace std;

struct end_t
{
	int x;
	int h;
	int type;
	int id;
};

bool cmp(const end_t& p, const end_t&q)
{
	if (p.x == q.x)
	{
		if (p.type == q.type)
			return p.id < q.id;

		return p.type > q.type;
	}

	return p.x < q.x;
}

struct h_t
{
	int h;
	int id;
};

struct hcmp
{
bool operator()(const h_t&p, const h_t& q)
{
	if (p.h == q.h)
		return p.id < q.id;
	else
		return p.h > q.h;
}
};
struct rect_t
{
	int x1, x2, h;
	int z1, z2;
};

int order(const int a, const int b, const int w, const int z)
{
    if (a > b)
    {   
        if (w > z)
            return order(b, a, z, w); 
        else
            return order(b, a, w, z); 
    }   

    if (w > z)
        return order(a, b, z, w); 
    
    if ((a < z && b > w) || (w < b && z > a)) 
        return 0;
    if (b <= w && a <= z)
        return -1; 
    return 1;
}

bool cho(const rect_t&p, const rect_t& q)
{
    int o1 = order(p.x1, p.x2, q.x1, q.x2);
    int o2 = order(p.z1, p.z2, q.z1, q.z2);

	if (o1 == 0 || o2 == 0 || o1 == o2)
		return false;
	else
		return true;
}

int main()
{
	int T;
	scanf("%i", &T);

	while (T --> 0)
	{
		int N,W;
		scanf("%i%i", &N, &W);

		rect_t tab[N];
		end_t list[N*2];

		for (int i=0; i<N; i++)
		{
			int a,b,c,d;
			scanf("%i%i%i%i", &a, &b, &c, &d);

			tab[i].x1 = min(a, c);
			tab[i].x2 = max(a, c);
			tab[i].h  = abs(b - d);
		}

		for (int i=0; i<N; i++)
		{
			int a,b,c,d;
			scanf("%i%i%i%i", &a, &b, &c, &d);
			tab[i].z1 = min(a, c);
			tab[i].z2 = max(a, c);
		}

		for (int i=0; i<N; i++)
		{
			if (tab[i].x1 <= tab[i].z1)
			{
				list[i*2].x = tab[i].x1;
				list[i*2].h = tab[i].h;
				list[i*2].type = 0;
				list[i*2].id = i;

				list[i*2+1].x = tab[i].z2;
				list[i*2+1].h = tab[i].h;
				list[i*2+1].type = 1;
				list[i*2+1].id = i;
			}
			else
			{
				list[i*2].x = tab[i].z1;
				list[i*2].h = tab[i].h;
				list[i*2].type = 0;
				list[i*2].id = i;

				list[i*2+1].x = tab[i].x2;
				list[i*2+1].h = tab[i].h;
				list[i*2+1].type = 1;
				list[i*2+1].id = i;
			}
		}

		sort(list, list+2*N, cmp);

		multiset<h_t, hcmp> heap;
		int h=0;
		bool failed = false;
		for (int i=0; i<N*2; i++)
		{
			if (list[i].type == 0)
			{
				if (heap.size() > 0 && ((*(heap.begin())).h + list[i].h) > W && cho(tab[(*(heap.begin())).id], tab[list[i].id]))
				{
					failed = true;
					break;
				}
				else
				{
					h_t tmp;
					tmp.h = list[i].h;
					tmp.id = list[i].id;
					heap.insert(tmp);
				}
			}
			else
			{
				h_t tmp;
				tmp.h = list[i].h;
				tmp.id = list[i].id;
				heap.erase(tmp);
			}
		}

		if (failed == false)
			printf("TAK\n");
		else
			printf("NIE\n");
	}
}