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
#include<cstdio>
#include<iostream>
#include<vector>
#include<climits>
#include<algorithm>
#include<map>
using namespace std;

class Car {


public:
	Car(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2){}

	void SetTargetPos(int px1, int py1, int px2, int py2) {
		tx1 = px1;
		tx2 = px2;
		ty1 = py1;
		ty2 = py2;
	}

	bool IsOriginalLeftFrom(Car other) {
		return x1 < other.x1;
	}

	bool IsTargetLeftFrom(Car other) {
		return tx1 < other.tx1;
	}

	bool CanPassBy(Car other, int parkHeight) {
		return height() + other.height() <= parkHeight;
	}

private:
	int x1, y1, x2, y2;
	int tx1, ty1, tx2, ty2;

	int height() {
		return y2 - y1;
	}

};

vector<Car> cars;
vector<Car> target;


bool sortFuncCars(int a, int b) {
	return cars[a].IsOriginalLeftFrom(cars[b]);
}

bool sortFuncTarget(Car a, Car b) {
	return a.IsTargetLeftFrom(b);
}

vector<int> carOrder;

int main() {

	int t, w, n, x1, y1, x2, y2;

	scanf("%d", &t);

	for (int i = 0; i < t; i++) {
		scanf("%d %d", &n, &w);
		for (int j = 0; j < n; j++) {
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			cars.push_back(Car(x1, y1, x2, y2));
			carOrder.push_back(j);
		}
		for (int j = 0; j < n; j++) {
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			cars[j].SetTargetPos(x1, y1, x2, y2);
		}
		sort(cars.begin(), cars.end(), sortFuncTarget);
		sort(carOrder.begin(), carOrder.end(), sortFuncCars);
		
		int canReorder = true;
		for (int i = 0; i < n; i++) { 
			int j = 0;
			while (j < n && carOrder[j] != i && canReorder) {
				if (carOrder[j] > i) {
					if (!cars[carOrder[j]].CanPassBy(cars[i], w)) {
						canReorder = false;
					}
				}
				j++;
			}
			if (!canReorder) break;
		}

		if (canReorder) printf("TAK\n");
		else printf("NIE\n");

		cars.clear();
		target.clear();
		carOrder.clear();
	}

}