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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.StringTokenizer;

/**
 * Created by Bogumil.
 */
public class par {

    public static void main(String[] args) throws Exception {
        Reader.init(System.in);
        PrintWriter pw = new PrintWriter(System.out);

        int T = Reader.nextInt();

        for (int t = 0; t < T; t++) {
            int n = Reader.nextInt();
            int w = Reader.nextInt();

            Car[] cars = new Car[n];

            int x1;
            int x2;
            int y1;
            int y2;
            for (int i = 0; i < n; i++) {
                cars[i] = new Car();
                x1 = Reader.nextInt();
                y1 = Reader.nextInt();
                x2 = Reader.nextInt();
                y2 = Reader.nextInt();

                if (x2 < x1) {
                    cars[i].x1 = x2;
                    cars[i].y1 = y2;
                    cars[i].x2 = x1;
                    cars[i].y2 = y1;
                } else {
                    cars[i].x1 = x1;
                    cars[i].y1 = y1;
                    cars[i].x2 = x2;
                    cars[i].y2 = y2;
                }
            }

            for (int i = 0; i < n; i++) {
                x1 = Reader.nextInt();
                y1 = Reader.nextInt();
                x2 = Reader.nextInt();
                y2 = Reader.nextInt();

                if (x2 < x1) {
                    cars[i].dx1 = x2;
                    cars[i].dy1 = y2;
                    cars[i].dx2 = x1;
                    cars[i].dy2 = y1;
                } else {
                    cars[i].dx1 = x1;
                    cars[i].dy1 = y1;
                    cars[i].dx2 = x2;
                    cars[i].dy2 = y2;
                }

                cars[i].init();
            }

            if (n <= 2000) {
                if (check(w, cars)) {
                    pw.println("TAK");
                } else {
                    pw.println("NIE");
                }
            } else {
                boolean result = firstPreCheck(w, cars);

                result = result && secondPreCheck(w, cars);

                if (result && probCheck(w, cars)) {
                    pw.println("TAK");
                } else {
                    pw.println("NIE");
                }
            }
        }

        pw.flush();
        pw.close();
        Reader.close();
    }

    static boolean firstPreCheck(int w, Car[] cars) {
        Arrays.sort(cars);

        int counts = Math.min(cars.length, (50000/cars.length) * 10);

        for (int i = 0; i < counts; i++) {
            for (int j = i+1; j < cars.length; j++) {
                if (cars[i].h+cars[j].h <= w) {
                    break;
                }

                if (!isOk(w, cars[i], cars[j])) {
                    return false;
                }
            }
        }

        return true;
    }

    static boolean secondPreCheck(int w, Car[] cars) {
        Arrays.sort(cars, MAX_DISTANCE);

        int counts = Math.min(cars.length, (50000/cars.length) * 10);

        for (int i = 0; i < counts; i++) {
            for (int j = i+1; j < cars.length; j++) {
                if (!isOk(w, cars[i], cars[j])) {
                    return false;
                }
            }
        }

        return true;
    }

    public static Comparator<Car> MAX_DISTANCE = new Comparator<Car>() {

        public int compare(Car c1, Car c2) {
            return -(Integer.valueOf(c1.dist).compareTo(c2.dist));
        }

    };

    static boolean probCheck(int w, Car[] cars) {
        Random r = new Random(System.currentTimeMillis());
        int idx1;
        int idx2;
        int n = cars.length;
        for (int t = 0; t < 2500000; t++) {
            idx1 = r.nextInt(n);
            idx2 = r.nextInt(n);
            while (idx1 == idx2) {
                idx2 = r.nextInt(n);
            }

            if (!isOk(w, cars[idx1], cars[idx2])) {
                return false;
            }
        }

        return true;
    }

    static boolean check(int w, Car[] cars) {
        Arrays.sort(cars);

        for (int i = 0; i < cars.length; i++) {
            for (int j = i+1; j < cars.length; j++) {
                if (cars[i].h+cars[j].h <= w) {
                    break;
                }

                if (!isOk(w, cars[i], cars[j])) {
                    return false;
                }
            }
        }

        return true;
    }

    static boolean isOk(int w, Car car1, Car car2) {
        if (car1.h+car2.h <= w) {
            return true;
        } else {
            Car startLeft;
            Car startRight;

            if (car1.x1 < car2.x1) { // Also car1.x2 <= car2.x1
                startLeft = car1;
                startRight = car2;
            } else {
                startLeft = car2;
                startRight = car1;
            }

            if (startLeft.dx2 > startRight.dx1) {
                return false;
            } else {
                return true;
            }
        }
    }

    static class Car implements Comparable<Car> {
        int h;
        int dist;

        int x1;
        int y1;
        int x2;
        int y2;

        int dx1;
        int dy1;
        int dx2;
        int dy2;

        public void init() {
            h = Math.abs(y2-y1);
            dist = Math.abs(dx1-x1);
        }

        @Override
        public int compareTo(Car o) {
            if (this.h == o.h) {
                return -(Integer.valueOf(this.dist).compareTo(o.dist));
            }
            return -(Integer.valueOf(this.h).compareTo(o.h));
        }
    }

    static class Reader {
        static BufferedReader reader;
        static StringTokenizer tokenizer;

        static void init(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        static String next() throws IOException {
            while (!tokenizer.hasMoreTokens() ) {
                tokenizer = new StringTokenizer(reader.readLine() );
            }
            return tokenizer.nextToken();
        }

        static int nextInt() throws IOException {
            return Integer.parseInt( next() );
        }

        static double nextDouble() throws IOException {
            return Double.parseDouble( next() );
        }

        static void close() throws IOException {
            reader.close();
        }
    }
}