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
#ifndef DEBUG
#define NDEBUG
#endif

#include <iostream>
#include <vector>
#include <assert.h>
#include <algorithm>

using namespace std;

struct Car
{
	int x1, y1, x2, y2;
	int width() const
	{
		return x2-x1;
	}
	int height() const
	{
		return y2-y1;
	}
};

bool cmp_by_height(const pair<Car,Car> &a, const pair<Car,Car> &b)
{
	return a.first.height() < b.first.height();
}

int n, w;
vector<pair<Car,Car>> cars;

istream &operator>>(istream &i, Car &c)
{
	cin >> c.x1 >> c.y1 >> c.x2 >> c.y2;
	assert(0 <= c.x1);
	assert(c.x1 <= 1000000000);
	assert(0 <= c.x2);
	assert(c.x2 <= 1000000000);
	assert(0 <= c.y1);
	assert(c.y1 <= w);
	assert(0 <= c.y2);
	assert(c.y2 <= w);
	assert(c.x1 != c.x2);
	assert(c.y1 != c.y2);
	if(c.x1 > c.x2)
	{
		swap(c.x1, c.x2);
	}
	if(c.y1 > c.y2)
	{
		swap(c.y1, c.y2);
	}
	return i;
}

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--)
	{
		cin >> n >> w;
		cars.resize(n);
		for(int i=0; i<n; i++)
		{
			cin >> cars[i].first;
		}
		for(int i=0; i<n; i++)
		{
			cin >> cars[i].second;
		}
		for(int i=0; i<n; i++)
		{
			assert(cars[i].first.width() == cars[i].second.width());
			assert(cars[i].first.height() == cars[i].second.height());
		}
#ifdef DEBUG
		for(int i=0; i<n; i++)
		{
			for(int j=i+1; j<n; j++)
			{
				if(
					((cars[i].first.x1 < cars[j].first.x2 && cars[i].first.x2 > cars[j].first.x1) ||
				     (cars[j].first.x1 < cars[i].first.x2 && cars[j].first.x2 > cars[i].first.x1)) &&
					((cars[i].first.y1 < cars[j].first.y2 && cars[i].first.y2 > cars[j].first.y1) ||
					 (cars[j].first.y1 < cars[i].first.y2 && cars[j].first.y2 > cars[i].first.y1))
				)
				{
					cerr << "Initial positions of cars " << i << " and " << j << " collides!" << endl;
				}
			}
		}
		for(int i=0; i<n; i++)
		{
			for(int j=i+1; j<n; j++)
			{
				if(
					((cars[i].second.x1 < cars[j].second.x2 && cars[i].second.x2 > cars[j].second.x1) ||
				     (cars[j].second.x1 < cars[i].second.x2 && cars[j].second.x2 > cars[i].second.x1)) &&
					((cars[i].second.y1 < cars[j].second.y2 && cars[i].second.y2 > cars[j].second.y1) ||
					 (cars[j].second.y1 < cars[i].second.y2 && cars[j].second.y2 > cars[i].second.y1))
				)
				{
					cerr << "Desired positions of cars " << i << " and " << j << " collides!" << endl;
				}
			}
		}
#endif
		sort(cars.begin(), cars.end(), &cmp_by_height);
		bool possible = true;
		for(int i=0; possible && i<n; i++)
		{
			int iw = cars[i].first.height();
			for(int j=n-1; possible && j > i && iw + cars[j].first.height() > w; j--)
			{
				if(
						(cars[i].first.x1  >= cars[j].first.x2 &&
						 cars[i].second.x2 <= cars[j].second.x1) ||
						(cars[i].first.x2  <= cars[j].first.x1 &&
						 cars[i].second.x1 >= cars[j].second.x2)
				  )
				{
					possible = false;
				}
			}
		}
		cout << (possible ? "TAK" : "NIE") << endl;
	}
	return 0;
}