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
/*
* Author Mateusz Bojanowski
* Parking 
*
*/

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>

// Helper definition
#define VAR(v, i) __typeof(i) v=(i)
#define FOR(i, j, k) for (int i = (j); i <= (k); ++i)
#define FORD(i, j, k)for (int i=(j); i >= (k); --i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define REP(i, n) for(int i = 0;i <(n); ++i)

#define N 50000
#define UN_INT unsigned int

/** Structure that define one car
*/

struct Car
{
	UN_INT height;
	UN_INT initPos;
	UN_INT endPos;
};

struct car_height {
	bool operator ()(Car const& a, Car const& b) const 
	{
        	if (a.height > b.height) return true;
         	return false;
        }
};

int main()
{
	UN_INT t = 0;
	UN_INT n = 0;
	UN_INT w = 0;
	UN_INT k = 0;

	UN_INT maxFirst = 0;
	UN_INT maxSecond= 0;
	UN_INT min = 0;
	
	UN_INT height = 0;
	UN_INT x1, x2, y1, y2;
	
	bool possible;

	Car cars[N];

	scanf("%d", &t);

	REP(i, t)
	{
		// Reset for each test
		possible = true;
		maxFirst = 0;
		maxSecond = 0;
		min = 0;

		scanf("%d %d", &n, &w);

		FOR (j, 0, n - 1)
		{	
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			height  = y1 < y2 ? y2 - y1: y1 - y2;
			
			cars[j].initPos = x1 < x2 ? x1: x2;
			cars[j].height = height;

			if (height > maxFirst) 
			{
				maxSecond = height;
				maxFirst = height;
			}
			else if (height > maxSecond)
				maxSecond = height;
		}

		
		FOR (j, 0, n - 1)
		{
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			cars[j].endPos = x1 < x2 ? x1: x2;

			if (cars[j].endPos != cars[j].initPos)
				possible = false;
		}
		
		if ( possible || n == 1)
		{
			printf("TAK\n");
			continue;
		}

		UN_INT j = 0;
		UN_INT k = n - 1;
		min = w - maxFirst;
		
		while(j < k)
		{
			if (cars[j].height <= min)
			{
				Car temp = cars[j];
				cars[j] = cars[k];
				cars[k] = temp;
				--k;
			}
			else
				++j;

		}
		
		std::sort(cars, cars + k + 1, car_height());
		
		j = 0;
		possible = true;
		while(cars[j].height * 2 > w && j < k)
		{
			FOR (l, j + 1, k)
			{
				if (cars[j].height + cars[l].height > w)
				{
					if (cars[j].initPos > cars[l].initPos && cars[j].endPos < cars[l].endPos)
					{
						possible = false;
						break;
					}

                                        if (cars[j].initPos < cars[l].initPos && cars[j].endPos > cars[l].endPos)
					{
					        possible = false;
					        break;
					}
				}
			}
			++j;
			if (!possible) break;
		}

		if (possible)
			printf("TAK\n");
		else
			printf("NIE\n");
	}
}