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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 
 */

/**
 * @author Kuba Straszewski
 * 
 */
public class par {

    public static void main(String[] args) {
        Reader reader = new Reader(System.in);
        Writer writer = new Writer();

        int tests = reader.nextInt();
        for (int t = 0; t < tests; t++) {
            boolean result = solve(reader);
            writer.println(result ? "TAK" : "NIE");
        }
        writer.flush();
    }

    static class MaxTree {
        int[] localTree;
        int n;

        MaxTree(int array[]) {
            n = 1;
            while (n < array.length)
                n *= 2;
            localTree = new int[n * 2 - 1];

            for (int i = 0; i < array.length; i++) {
                localTree[n - 1 + i] = array[i];
            }
            for (int i = n - 2; i >= 0; i--) {
                localTree[i] = Math.max(localTree[i * 2 + 1], localTree[i * 2 + 2]);
            }
        }

        int max(int left, int right) {
            return max(left, right, n, 0);
        }

        int max(int left, int right, int k, int offset) {
            if (left == 0 && right == k - 1) {
                return localTree[offset];
            }
            boolean ll = left < k / 2;
            boolean rl = right < k / 2;

            int lo = offset * 2 + 1;
            int ro = offset * 2 + 2;

            if (ll && rl) {
                return max(left, right, k / 2, lo);
            }
            if (!ll && !rl) {
                return max(left - k / 2, right - k / 2, k / 2, ro);
            }
            int lm = max(left, k / 2 - 1, k / 2, lo);
            int rm = max(0, right - k / 2, k / 2, ro);
            return Math.max(lm, rm);
        }
    }

    private static boolean solve(Reader reader) {
        int n = reader.nextInt();
        int w = reader.nextInt();

        Auto[] auta = new Auto[n];

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

            auta[i] = new Auto(Math.min(x1, x2), Math.abs(y1 - y2));
        }

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

            auta[i].finalLeft = Math.min(x1, x2);
        }

        List<Auto> duzeAuta = new LinkedList<Auto>();
        List<Auto> maleAuta = new LinkedList<Auto>();

        for (int i = 0; i < auta.length; i++) {
            Auto auto = auta[i];

            if (auto.height * 2 > w) {
                duzeAuta.add(auto);
            } else {
                maleAuta.add(auto);
            }
        }

        if (duzeAuta.isEmpty())
            return true;

        Collections.sort(duzeAuta, new Comparator<Auto>() {
            @Override
            public int compare(Auto arg0, Auto arg1) {
                return Integer.compare(arg0.left, arg0.left);
            }
        });

        if (!sorted(duzeAuta)) {
            return false;
        }

        if (!maleAutaFits(maleAuta, duzeAuta, w)) {
            return false;
        }

        return true;
    }

    private static boolean maleAutaFits(List<Auto> maleAuta, List<Auto> duzeAutaList, int w) {
        Auto[] duzeAuta = duzeAutaList.toArray(new Auto[0]);

        int[] wysokosci = new int[duzeAuta.length];
        for (int i = 0; i < duzeAuta.length; i++) {
            wysokosci[i] = duzeAuta[i].height;
        }
        MaxTree maxtree = new MaxTree(wysokosci);

        for (Auto auto : maleAuta) {

            // znajdz indeks auta po lewej na starcie
            int startingLeftIndex = startingLeftIndex(duzeAuta, auto);

            // znajdz indeks auta po lewej na koncu

            int finalLeftIndex = finalLeftIndex(duzeAuta, auto);

            int biggestCar = biggestCar(startingLeftIndex, finalLeftIndex, maxtree);

            if (biggestCar + auto.height > w) {
                return false;
            }
        }

        return true;

    }

    // indeksy auta po lewej przed i po
    private static int biggestCar(int a, int b, MaxTree tree) {
        int l = Math.min(a, b);
        int r = Math.max(a, b);
        if (a == b)
            return 0;
        return tree.max(l + 1, r);
    }

    private static int finalLeftIndex(Auto[] duzeAuta, Auto auto) {
        int left = -1;
        int right = duzeAuta.length;
        while (right > left + 1) {
            int middle = (right + left) / 2;
            if (auto.finalLeft >= duzeAuta[middle].finalLeft) {
                left = middle;
            } else {
                right = middle;
            }
        }
        return left;
    }

    private static int startingLeftIndex(Auto[] duzeAuta, Auto auto) {
        int left = -1; // auto na pewno po lewej
        int right = duzeAuta.length; // auto na pewno po prawej

        while (right > left + 1) {
            int middle = (right + left) / 2;
            if (auto.left >= duzeAuta[middle].left) {
                left = middle;
            } else {
                right = middle;
            }
        }
        return left;
    }

    private static boolean sorted(List<Auto> auta) {
        Auto previous = auta.get(0);
        for (Auto auto : auta) {
            if (previous.finalLeft > auto.finalLeft) {
                System.err.println("NIE, bo " + previous + " nie sie przecina z " + auto);
                return false;
            }
            previous = auto;
        }
        return true;
    }

    static class Auto {
        int left;
        int height;
        int finalLeft;

        public Auto(int left, int height) {
            this.left = left;
            this.height = height;
        }
    }

    static class Writer {
        private BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

        void print(String str) {
            try {
                out.write(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        void println(String str) {
            try {
                out.write(str);
                out.write("\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        void flush() {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    static class Reader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        Reader(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        String next() {
            if (tokenizer.hasMoreTokens()) {
                return tokenizer.nextToken();
            }
            String str;
            try {
                str = reader.readLine();
                if (str == null)
                    return null;
                tokenizer = new StringTokenizer(str);
                return next();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }

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