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
/*
 * main.cpp
 *
 *  Created on: 14-05-2014
 *      Author: adasdeb
 */

#include <iostream>

struct inter
{
	int xl;
	int xr;

	inter(int x1 = 0, int x2 = 0)
		: xl((x1 < x2 ? x1 : x2)), xr((x1 > x2 ? x1 : x2)) {}

	bool isin(int x) const
	{
		return x > xl && x < xr;
	}
	bool operator==(const inter &roper) const
	{
		return (xl == roper.xl) && (xr == roper.xr);
	}
	bool operator^(const inter &roper) const
	{
		return 	!(((xr <= roper.xl) && (xl <= roper.xl)) ||
				  ((xr >= roper.xr) && (xl >= roper.xr)));
	}
	bool operator>(const inter &roper) const
	{
		return (xl < roper.xl) && (xr > roper.xr);
	}
	bool operator>=(const inter &roper) const
	{
		return (*this > roper) || (*this == roper);
	}
	bool operator<(const inter &roper) const
	{
		return (xl > roper.xl) && (xr < roper.xr);
	}
	bool operator<=(const inter &roper) const
	{
		return (*this < roper) || (*this == roper);
	}
};

int maxel(int a, int b);
int minel(int a, int b);

typedef inter carb;

struct car
{
	carb bound;
	int shift;
	int w;

	car(int x1 = 0, int x2 = 0, int w = 0)
		: bound(carb(x1,x2)), shift(0), w(w) {}

	bool operator!=(const car &cr) const
	{
		return
		inter(bound.xl + shift, bound.xr + shift)^
		inter(minel(cr.bound.xl, cr.bound.xl + cr.shift),
			  maxel(cr.bound.xr, cr.bound.xr + cr.shift));
	}
};

int main(void)
{
	int ds;
	std::cin.sync_with_stdio(false);
	std::cin >> ds;

	while(ds--)
	{
		int carsn, parkw;
		std::cin >> carsn >> parkw;
		car cars[carsn];
		bool possible = true;

		for(int i = 0; i < carsn; i++)
		{
			int x1, y1, x2, y2;
			std::cin >> x1 >> y1 >> x2 >> y2;
			cars[i].bound.xl = minel(x1,x2);
			cars[i].bound.xr = maxel(x1,x2);
			cars[i].w = maxel(y1,y2)-minel(y1,y2);
		}
		for(int i = 0; i < carsn; i++)
		{
			int x1, y1, x2, y2;
			std::cin >> x1 >> y1 >> x2 >> y2;
			cars[i].shift = minel(x1,x2) - cars[i].bound.xl;
		}

		for(int i = 0; i < carsn; i++)
		{
			for(int j = i + 1; j < carsn; j++)
			{
				if(cars[i]!=cars[j])
				{
					if(cars[i].w + cars[j].w > parkw)
					{
						possible = false;
						goto exitloop;
					}
				}
			}
		}exitloop:

		if(possible)
			std::cout << "TAK\n";
		else
			std::cout << "NIE\n";
	}

	return 0;
}

int maxel(int a, int b)
{
	return (a > b ? a : b);
}

int minel(int a, int b)
{
	return (a < b ? a : b);
}