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
152
153
154
155
156
157
158
159
/**
 * @author Tadeusz Faber (SI-Consulting SA).
 * 
 * Potyczki Algorytmiczne 2014.
 * 
 * 2014-05-14 Runda 3B. Parking. 
 *  
 * Pamiec: 128MB.
 * Czas: ?s.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.Comparator;

class Car {
	private int x1;
	private int x2;
	private int w;
	private int z1;
	private int z2;

	public Car(int x1, int y1, int x2, int y2) {
		this.x1 = Math.min(x1, x2);
		this.x2 = Math.max(x1, x2);
		this.w = Math.max(y1, y2) - Math.min(y1, y2);
	}

	public void setDestination(int x1, int y1, int x2, int y2) {
		this.z1 = Math.min(x1, x2);
		this.z2 = Math.max(x1, x2);
	}

	public int getW() {
		return w;
	}

	public boolean check(Car car, int w) {
		if (this.w + car.w <= w) {
			return true;
		} else {
			int a1 = Math.min(x1, z1);
			int a2 = Math.max(x2, z2);
			int b1 = Math.min(car.x1, car.z1);
			int b2 = Math.max(car.x2, car.z2);
			if ((a1 >= b1 && a1 <= b2) || (a2 >= b1 && a2 <= b2)
					|| (a1 <= b1 && a1 >= b2)) {
				return false;
			} else {
				return true;
			}
		}
	}
}

class CarComparator implements Comparator<Car> {

	@Override
	public int compare(Car c1, Car c2) {
		if (c2 == null)
			return -1; // c1 < c2
		if (c1.getW() > c2.getW())
			return 1; // c1 > c2
		else if (c1.getW() < c2.getW())
			return -1; // c1 < c2
		else
			return 0;
	}
}

public final class par {

	private static Reader in = null;

	/**
	 * Zwroc kolejna liczbe calkowita ze strumienia in
	 * 
	 * @return wczytana liczba
	 */
	private static int nextInteger() {

		int r = 0;
		boolean minus = false;
		char c;

		try {
			do {
				c = (char) in.read();
			} while (c == ' ' || c == '\n' || c == '\r');
			if (c == '-') {
				minus = true;
				c = (char) in.read();
			}
			do {
				r = r * 10 + (int) c - 48;
				c = (char) in.read();
			} while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (minus) {
			return -r;
		} else {
			return r;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {

		in = new BufferedReader(new InputStreamReader(System.in));

		int t = nextInteger(); // 1..20
		for (int ti = 1; ti <= t; ti++) {
			int n = nextInteger(); // 1..50 000
			int w = nextInteger(); // 1..1 000 000 000
			Car[] cars = new Car[n];
			int maxW = 0;
			for (int i = 0; i < n; i++) {
				cars[i] = new Car(nextInteger(), nextInteger(), nextInteger(),
						nextInteger());
				maxW = Math.max(maxW, cars[i].getW());
			}
			for (int i = 0; i < n; i++) {
				cars[i].setDestination(nextInteger(), nextInteger(),
						nextInteger(), nextInteger());
			}

			if (w >= 2 * maxW) {
				System.out.println("TAK");
			} else {
				CarComparator comparator = new CarComparator();
				Arrays.sort(cars, comparator);
				boolean ok = true;
				for (int i = 0; i < n && ok; i++) {
					boolean next = true;
					for (int j = 0; j < n && next && ok; j++) {
						if (!cars[i].check(cars[j], w)) {
							ok = false;
						}
						if (cars[i].getW() + cars[j].getW() <= w) {
							next = false;
						}
					}
				}
				if (ok) {
					System.out.println("TAK");
				} else {
					System.out.println("NIE");
				}
			}
		}
	}
}