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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public final class par {
	public static InputStream INPUT_STREAM = System.in;
	public static PrintStream PRINT_STREAM = System.out;

	private static final String SEPARATOR = " ";

	private static final void print(String result) {
		PRINT_STREAM.println(result);
	}

	private final static class Car {

		public Car(int s1, int height) {
			this.s1 = s1;
			this.height = height;
		}

		int s1;

		int d1;

		int startIndex;

		int height;

	}

	private final static Comparator<Car> byStartPositionComparator = new Comparator<Car>() {

		@Override
		public int compare(Car arg0, Car arg1) {

			int sBegin = arg0.s1 - arg1.s1;

			return sBegin;
		}
	};

	private final static Comparator<Car> byEndPositionComparator = new Comparator<Car>() {

		@Override
		public int compare(Car arg0, Car arg1) {
			int begin = arg0.d1 - arg1.d1;
			return begin;
		}
	};

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(INPUT_STREAM));

		int testsNumber = Integer.parseInt(br.readLine());

		for (int t = 0; t < testsNumber; t++) {
			String[] tmp = br.readLine().split(SEPARATOR);
			int carsNumbers = Integer.parseInt(tmp[0]);
			int PARKING_HEIGHT = Integer.parseInt(tmp[1]);

			Car[] cars = new Car[carsNumbers];

			for (int i = 0; i < carsNumbers; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());

				int tmps = Integer.parseInt(st.nextToken());

				int h1 = Integer.parseInt(st.nextToken());
				st.nextToken();
				int height = Math.abs(h1 - Integer.parseInt(st.nextToken()));
				cars[i] = new Car(tmps, height);
			}

			for (int i = 0; i < carsNumbers; i++) {
				// tmp = br.readLine().split(SEPARATOR);

				cars[i].d1 = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken());

			}

			// System.out.println(Arrays.toString(cars));

			solve(cars, PARKING_HEIGHT);

		}
	}

	private static void solve(Car[] startComposition, int PARKING_HEIGHT) {
		prepareStartComposition(startComposition);
		Car[] endComposition = prepareEndComposition(startComposition);

		// System.out.println(String.format("By Start: %s",
		// Arrays.toString(startComposition)));
		// System.out.println(String.format("By End:   %s\n",
		// Arrays.toString(endComposition)));

		;

		print(//
		solveEasyOne(startComposition, PARKING_HEIGHT) || //
				solve(startComposition, endComposition, PARKING_HEIGHT)

		? "TAK" : "NIE" //
		);
	}

	private static boolean solveEasyOne(Car[] startComposition, int PARKING_HEIGHT) {
		int max = startComposition[0].height;
		for (int i = 1; i < startComposition.length; i++) {
			if (startComposition[i].height + max <= PARKING_HEIGHT) {
				if (startComposition[i].height > max) {
					max = startComposition[i].height;
				}
			} else {
				return false;
			}
		}

		return true;
	}

	private static void prepareStartComposition(Car[] startComposition) {
		Arrays.sort(startComposition, byStartPositionComparator);

		for (int i = 0; i < startComposition.length; i++) {
			startComposition[i].startIndex = i;
		}
	}

	private static Car[] prepareEndComposition(Car[] startComposition) {
		Car[] endComposition = new Car[startComposition.length];

		System.arraycopy(startComposition, 0, endComposition, 0, startComposition.length);

		Arrays.sort(endComposition, byEndPositionComparator);
		return endComposition;
	}

	private static boolean solve(Car[] startComposition, Car[] endComposition, int PARKING_HEIGHT) {
		for (Car car : endComposition) {
			for (int j = 0; j < car.startIndex; j++) {
				if (//
				(startComposition[j].d1 > car.d1) && (startComposition[j].height + car.height > PARKING_HEIGHT) //
				) {

					return false;
				}
			}
		}

		return true;
	}

}