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

using namespace std;

int D[1<<17];
int n,M;

void buildTree()
{
	M = 1;
	while(n > M)
		M <<= 1;
	for(int i = 1;i<2*M;i++)
		D[i] = 0;
}

void update(int k, int v)
{
	k += M;
	D[k] = v;
	k >>= 1;
	while(k >= 1)
	{
		D[k] = max(D[2*k], D[2*k + 1]);
		k >>= 1;
	}
}

int query(int p, int q)
{
	p += M;
	q += M;
	int result = 0;
	while(q > p)
	{
		if(p&1)
			result = max(result, D[p++]);
		if(!(q&1))
			result = max(result, D[q--]);
		p >>= 1;
		q >>= 1;
	}
	if(p == q)
		result = max(result, D[p]);
	return result;
}

struct car
{
	int x, id, height;
	car(){}
	car(int x, int id, int height) : x(x), id(id), height(height) {}
};

inline bool operator<(const car &a, const car &b)
{
	return a.x < b.x;
}

int position[1<<17];
car from[1<<17];
car to[1<<17];
int w;

void read()
{
	scanf("%d%d", &n, &w);
	for(int i = 0;i<n;i++)
	{
		int x1, x2, y1, y2;
		scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
		from[i] = car(min(x1,x2), i, max(y1,y2) - min(y1,y2));
	}
	for(int i = 0;i<n;i++)
	{
		int x1,x2,y1,y2;
		scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
		to[i] = car(min(x1,x2), i, max(y1,y2) - min(y1,y2));
	}
	sort(from, from + n);
	sort(to, to + n);
}

void getPosition()
{
	for(int i = 0;i<n;i++)
		position[from[i].id] = i;
}

bool check(int k)
{
	if(query(position[to[k].id], n-1) > w - to[k].height)
		return false;
	update(position[to[k].id], to[k].height);
	return true;
}

void solve()
{
	getPosition();
	for(int i = 0;i<n;i++)
	{
		if(!check(i))
		{
			printf("NIE\n");
			return;
		}
	}
	printf("TAK\n");
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		read();
		solve();
	}
	return 0;
}