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
import java.util.*;

public class par {
    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new par().start();
    }

    TreeSet<Car> before = new TreeSet<Car>(new Comparator<Car>() {
        public int compare(Car o1, Car o2) {
            return o1.posX > o2.posX ? 1 : -1;
        }
    });

    TreeSet<Car> after = new TreeSet<Car>(new Comparator<Car>() {
        public int compare(Car o1, Car o2) {
            return o1.posX2 > o2.posX2 ? 1 : -1;
        }
    });

    private void start() {
        int x1, y1, x2, y2;
        test:
        for (int t = sc.nextInt(); t > 0; t--) {
            int n = sc.nextInt();
            int w = sc.nextInt();

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

                c.posX = x1;
                c.width = y2 - y1;
                carTab[i] = c;
                before.add(c);
            }

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

                carTab[i].posX2 = x1;
                after.add(carTab[i]);
            }

            Iterator<Car> iter = after.iterator();
            while (iter.hasNext()) {
                Car c = iter.next();
                SortedSet<Car> pre = before.headSet(c, true);
                Iterator<Car> iter2 = pre.iterator();
                while (iter2.hasNext()) {
                    Car c2 = iter2.next();
                    if (c2 != c && !(c2.posX2 < c.posX2) && (c2.width + c.width > w)) {
                        System.out.println("NIE");
                        continue test;
                    }
                }
            }
            System.out.println("TAK");
        }
    }

    class Car {
        public int posX;
        public int width;
        public int posX2;
    }
}