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

using namespace std;

#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(x) memset(x,0,sizeof(x))

typedef long long LL;
typedef unsigned long long ULL;

const int INF = 1000000000;

const int MAX = 50000;
int XY[MAX][5];
int O[MAX], S[MAX], E[MAX];
int PS[MAX], PE[MAX];

bool compO(int x, int y) 
{ 
	return XY[x][4] > XY[y][4] || (XY[x][4] == XY[y][4] && XY[x][0] < XY[y][0]) || (XY[x][4] == XY[y][4] && XY[x][0] == XY[y][0] && x < y);
}

bool compSE(int x, int y) 
{ 
	return XY[x][0] < XY[y][0] || (XY[x][0] == XY[y][0] && XY[x][2] < XY[y][2]) || (XY[x][0] == XY[y][0] && XY[x][2] == XY[y][2] && x < y);
}

struct Node
{
	int elem;
	Node* left;
	Node* right;
};

Node* createNode(int elem)
{
	Node* node = new Node();
	node->elem = elem;
	node->left = node->right = NULL;

	return node;
}

int main(int argc, char **args)
{
	int t, n, w, x1, y1, x2, y2;

	scanf("%d", &t);

	REP(i, t)
	{
		scanf("%d %d", &n, &w);

		REP(i, n)
		{
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

			XY[i][0] = min(x1, x2);
			XY[i][1] = max(x1, x2);
			XY[i][2] = min(y1, y2);
			XY[i][3] = max(y1, y2);
			XY[i][4] = XY[i][3] - XY[i][2];
			O[i] = S[i] = E[i] = i;
		}

		sort(O, O + n, compO);
		sort(S, S + n, compSE);
	
		REP(i, n)
		{
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

			XY[i][0] = min(x1, x2);
			XY[i][1] = max(x1, x2);
			XY[i][2] = min(y1, y2);
			XY[i][3] = max(y1, y2);
		}

		sort(E, E + n, compSE);

		REP(i, n)
		{
			PS[S[i]] = i;
			PE[E[i]] = i;
		}

		Node* root = NULL;

		bool found = true;

		REP(i, n)
		{
			if (root == NULL)
			{
				root = createNode(O[i]);
				continue;
			}

			if (XY[root->elem][4] + XY[O[i]][4] <= w)
				break;

			Node* cur = root;

			while (true)
			{
				if (XY[cur->elem][4] + XY[O[i]][4] <= w)
					break;

				if (PS[O[i]] < PS[cur->elem] && PE[O[i]] < PE[cur->elem])
				{
					if (cur->left == NULL)
					{
						cur->left = createNode(O[i]);
						break;
					}

					cur = cur->left;
				}
				else if (PS[O[i]] > PS[cur->elem] && PE[O[i]] > PE[cur->elem])
				{
					if (cur->right == NULL)
					{
						cur->right = createNode(O[i]);
						break;
					}

					cur = cur->right;
				}
				else
				{
					found = false;
					break;
				}
			}

			if (!found)
				break;
		}

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

	return 0;
}