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
160
161
162
163
164
165
166
167
168
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;


public class par {
	
	int testNumber;
	
	int testCarNumber[] = new int[20];
	int testParkingHeight[] = new int[20];
	
	Car testStartCars[][] = new Car[20][50000];
	Car testTargetCars[][] = new Car[20][50000];
			
	boolean[] results = new boolean[20];
	
	public static void main(String[] args) {
		new par();
	}
	
	void process() {
		for (int i = 0; i < testNumber; i++) {
			int carNumber = testCarNumber[i]; 
			int parkingHeight = testParkingHeight[i];
			Car[] startCars = testStartCars[i];
			Car[] targetCars = testTargetCars[i];
			
			boolean result = check(carNumber, parkingHeight, startCars, targetCars);
		
			results[i] = result;
		}
	}
	
	boolean check(int carNumber, int parkingHeight, Car[] startCars, Car[] targetCars) {
		Comparator<Car> carComparator = new CarComparator(parkingHeight);
		Arrays.sort(startCars, 0, carNumber, carComparator);
		Arrays.sort(targetCars, 0, carNumber, carComparator);
	
		for (int i = 0; i < carNumber; i++) {
			if (startCars[i].getId() != targetCars[i].getId()) {
				return false;
			}
		}
		
		return true;
	}
	
	public par() {
		read();
		process();
		print();
	}
	
	void read() {
		InputStream is = System.in;
//		if (Boolean.getBoolean("test")) {
//			is = this.getClass().getResourceAsStream("par.in");
//		}
		
		BufferedInputStream bis = new BufferedInputStream(is);

		testNumber = readInt(bis);
		
		for (int i = 0; i < testNumber; i++) {
			testCarNumber[i] = readInt(bis);
			testParkingHeight[i] = readInt(bis);
		
			readCars(testStartCars[i], testCarNumber[i], bis);
			readCars(testTargetCars[i], testCarNumber[i], bis);
		}
	}

	void readCars(Car cars[], int carNumber, BufferedInputStream bis) {
		for (int j = 0; j < carNumber; j++) {
			int x1 = readInt(bis);
			int y1 = readInt(bis);
			int x2 = readInt(bis);
			int y2 = readInt(bis);
			
			Car car = new Car(j, Math.min(x1, x2), Math.abs(y1-y2));
			
			cars[j] = car;
		}
	}
	
	void print() {
		for (int i = 0; i < testNumber; i++) {
			String result = results[i]?"TAK":"NIE";
			System.out.println(result);
		}
	}
	
	class CarComparator implements Comparator<Car> {

		private final int parkingHeight;
		
		public CarComparator(int parkingHeight) {
			super();
			this.parkingHeight = parkingHeight;
		}

		@Override
		public int compare(Car c1, Car c2) {
			if (c1.getHeight() + c2.getHeight() <= parkingHeight) {
				if(c1.getHeight() < c2.getHeight()) return -1;
				if(c1.getHeight() > c2.getHeight()) return 1;
				if(c1.getId() < c2.getId()) return -1;
				if(c1.getId() > c2.getId()) return 1;
			} else {
				if(c1.getPosition() < c2.getPosition()) return -1;
				if(c1.getPosition() > c2.getPosition()) return 1;
			}
			
			return 0;
		}
	}
	
	class Car {
		private final int id;
		private final int position;
		private final int height;

		public Car(int id, int position, int height) {
			super();
			this.id = id;
			this.position = position;
			this.height = height;
		}
		
		public int getId() {
			return id;
		}
		public int getPosition() {
			return position;
		}
		public int getHeight() {
			return height;
		}
		@Override
		public String toString() {
			return "Car [id=" + id + ", position=" + position + ", height="
					+ height + "]";
		}
	}
	
	private int readInt(InputStream in) {
		int ret = 0;
		boolean dig = false;
		
		try {
			for (int c = 0; (c = in.read()) != -1;) {
				if (c >= '0' && c <= '9') {
					dig = true;
					ret = ret * 10 + c - '0';
				} else if (dig) {
					break;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		return ret;
	}
}