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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long int lint;

struct box
{
	int start;
	int end;
	int height;

	struct cmp
	{
		inline bool operator () (const box& a, const box& b) const
		{
			return a.start < b.start;
		}
	};
};

int w;
int* maxHeights;
box* swapBoxes;

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

void calcMaxHeights(int* out, box* boxes, int size)
{
	out[size - 1] = boxes[size - 1].height;
	for (int i = size - 2; i >= 0; --i)
		out[i] = max(out[i + 1], boxes[i].height);
}

bool mergeSort(box* boxes, int size)
{
	if (size <= 1)
		return true;

	const int size0 = size / 2;
	const int size1 = size - size0;
	if (size0 > 1 && !mergeSort(boxes, size0))
		return false;
	if (size1 > 1 && !mergeSort(boxes + size0, size1))
		return false;

	calcMaxHeights(maxHeights, boxes, size0);

	int curr0 = 0;
	int curr1 = size0;
	for (int i = 0; i < size; i++)
	{
		if (curr0 < size0 && (curr1 == size || boxes[curr0].end <= boxes[curr1].end))
		{
			swapBoxes[i] = boxes[curr0++];
		}
		else
		{
			if (curr0 < size0 && w - boxes[curr1].height < maxHeights[curr0])
				return false;

			swapBoxes[i] = boxes[curr1++];
		}
	}

	for (int i = 0; i < size; i++)
		boxes[i] = swapBoxes[i];
	return true;
}

void solveCase()
{
	int n;
	cin >> n >> w;

	maxHeights = new int[n];
	swapBoxes = new box[n];

	vector<box> boxes;
	boxes.reserve(n);
	for (int i = 0; i < n; i++)
	{
		box b;
		int bottom, top, right;
		cin >> b.start >> bottom >> right >> top;
		b.height = top - bottom;

		boxes.push_back(b);
	}
	for (int i = 0; i < n; i++)
	{
		int bottom, top, right;
		cin >> boxes[i].end >> bottom >> right >> top;
	}

	sort(boxes.begin(), boxes.end(), box::cmp());

	const bool possible = mergeSort(&boxes[0], boxes.size());
	cout << (possible ? "TAK\n" : "NIE\n");

	delete[] maxHeights;
	delete[] swapBoxes;
}

int main()
{
	int t;
	cin >> t;

	for (int i = 0; i < t; i++)
		solveCase();

	return 0;
}