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
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;

public class par {
    private static int skipWS(final InputStream in) throws IOException {
		int val = -1;
		while ((val = in.read()) != -1) {
			if (!Character.isWhitespace((char) val)) break;
		}
		return val;
	}

	private static int readInt(final InputStream in) {
		try {
			final StringBuilder b = new StringBuilder();
			int val = skipWS(in);
			b.append((char) val);
			while ((val = in.read()) != -1) {
				if (Character.isWhitespace((char) val))
					break;
				b.append((char) val);
			}
			return Integer.parseInt(b.toString());
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}


	public static void main(String[] args) throws Exception {
		final int t = readInt(System.in);
		for (int i = 0; i < t; i++) {
			parking(readInt(System.in), readInt(System.in));
		}
	}

	private static void parking(int n, int w) {
		Car[] cars = new Car[n];

		TreeMap<Integer, Integer> slots = new TreeMap<>();
		for (int i = 0; i < n; i++) {
			cars[i] = new Car();
			Integer slotIdx = cars[i].sourceX;
			if (slots.get(slotIdx) == null) {
				slots.put(slotIdx, cars[i].wysokosc);
			} else {
				int currentH = slots.get(slotIdx);
				if (currentH < cars[i].wysokosc) {
					slots.put(slotIdx, cars[i].wysokosc);
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cars[i].targetX = readInt(System.in);
			readInt(System.in); readInt(System.in); readInt(System.in);
		}
		int sourceX, targetX;
		for (int i = 0; i < n; i++) {
			sourceX = cars[i].sourceX;
			targetX = cars[i].targetX;
			if (sourceX == targetX) continue;
			boolean goDown = targetX < sourceX;
			Map.Entry<Integer, Integer> currentSlot = goDown ? slots.floorEntry(sourceX-1) : slots.ceilingEntry(sourceX+1);
			do {
				if (w - currentSlot.getValue() < cars[i].wysokosc) {
					System.out.println("NIE");
					return;
				}
				currentSlot = goDown ? slots.lowerEntry(currentSlot.getKey()) : slots.higherEntry(currentSlot.getKey());
			} while (currentSlot != null && (goDown? currentSlot.getKey() > targetX : currentSlot.getKey() < targetX));
		}
		System.out.println("TAK");
	}

	private static class Car {
		final int sourceX;
		final int siegaDoX;
		final int wysokosc;
		int targetX;

		public Car() {
			sourceX = readInt(System.in);
			int y1 = readInt(System.in);
			siegaDoX = readInt(System.in); //x2 is ignored
			wysokosc = readInt(System.in) - y1;
		}
	}
}