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

/**
 * @author michalsk
 */
public class par {

    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        int tests = Reader.nextInt();
        int test = 0;
        while (test < tests) {
            boolean fail = false;
            int cars = Reader.nextInt();
            int x1sO[] = new int[cars];
            int x2sO[] = new int[cars];
            int hO[] = new int[cars];

            int x1sNew[] = new int[cars];
            int x2sNew[] = new int[cars];
            int hNew[] = new int[cars];
            long totalHight = Reader.nextInt();
            int carNo = 0;
            // read old
            while (carNo < cars) {
                x1sO[carNo] = Reader.nextInt();
                int h1 = Reader.nextInt();
                x2sO[carNo] = Reader.nextInt();
                int h2 = Reader.nextInt();
                hO[carNo] = h2 - h1;

                carNo++;
            }
            // read new
            carNo = 0;
            while (carNo < cars) {
                x1sNew[carNo] = Reader.nextInt();
                int h1 = Reader.nextInt();
                x2sNew[carNo] = Reader.nextInt();
                int h2 = Reader.nextInt();
                hNew[carNo] = h2 - h1;
                carNo++;
            }

            test:
            for (int i = 0; i < cars; i++) {
                int hight = hO[i];
                if (2 * hight > totalHight) {

                    for (int j = 0; i != j && j < cars; j++) {
                        int h2 = hNew[j];
                        if (hight + h2 > totalHight) {
                            int oldCar2x1 = x1sO[j];
                            int newCar2x1 = x1sNew[j];
                            int oldCarx1 = x1sO[i];
                            int newCarx1 = x1sNew[i];
                            if ((oldCarx1 < oldCar2x1) != (newCarx1 < newCar2x1)) {
                                fail = true;
                                break test;
                            }
                        }
                    }
                }
            }

            System.out.println(fail ? "NIE" : "TAK");
            test++;
        }
    }

    /** Class taken from http://www.cpe.ku.ac.th/~jim/java-io.html */
    static class Reader {

        static BufferedReader reader;
        static StringTokenizer tokenizer;

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        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());
        }

    }

}