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
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

struct ST {
    int n;
    int *tab;

    int findpow(int x) const {
        int y = 1;
        while(y<x) y*=2;

        return y;
    }

   ST(int _n) : n(findpow(_n)) {
        tab = new int[2*n];
   }

   void update_node(int x) {
        tab[x] = max(tab[2*x], tab[2*x+1]);
   }

   void fill(int *in) {
       for(int i=0; i<n; ++i)
            tab[i+n] = in[i];
       for(int i=n-1; i>=1; --i)
            update_node(i);
   }

   void print_tree() {
        for(int i=1; i<=n; i*=2) {
            for(int j=0; j<i; ++j)
                printf("%*d", 3*(n/i), tab[i+j]);
            printf("\n");
        }
   }

   ~ST() { delete [] tab; }

   int query(int A, int B) {
        int a = A+n, b = B+n;
        int res = 0;

        while(a<b) {

            if(a%2 == 1) res = max(tab[a], res);
            if(b%2 == 1) res = max(tab[b-1], res);

            a = (a+1)/2, b = b/2;
        }

       return res;
   }

   void set(int A, int val) {
        int a = A+n;
        tab[a] = val;
        a/=2;
        while(a) {
            update_node(a);
            a/=2;
        }
   }
};

struct car
{
    int id;
    int h;
    int begin;
    int end;
};

bool cmp (car a, car b)
{
    return a.begin < b.begin;
}

int p [524288];
car tab1 [500000];
car tab2 [500000];

int main ()
{
    int t;
    int n, w;
    int i, j;
    int tmpx1, tmpy1, tmpx2, tmpy2;
    map <int, int> idp;
    scanf ("%d", &t);
    for (j = 0; j < t; ++ j)
    {
        scanf ("%d %d", &n, &w);
        ST tree (n);
        for (i = 0; i < n; ++ i)
        {
            scanf ("%d %d %d %d", &tmpx1, &tmpy1, &tmpx2, &tmpy2);
            tab1 [i].id = i;
            tab1 [i].begin = tmpx1;
            tab1 [i].end = tmpx2;
            tab1 [i].h = tmpy2 - tmpy1;
        }
        sort (tab1, tab1 + n, cmp);
        for (i = 0; i < n; ++ i)
        {
            scanf ("%d %d %d %d", &tmpx1, &tmpy1, &tmpx2, &tmpy2);
            tab2 [i].id = i;
            tab2 [i].begin = tmpx1;
            tab2 [i].end = tmpx2;
            tab2 [i].h = tmpy2 - tmpy1;
        }
        sort (tab2, tab2 + n, cmp);
        for (i = 0; i < n; ++ i)
        {
            idp [tab2 [i].id] = i;
            p [i] = tab2 [i].h;
        }
        tree.fill (p);
        //tree.print_tree ();
        bool flag = 1;
        for (i = n - 1; i != 0; -- i)
        {
            int pos = idp [tab1 [i].id];
            if (pos != i)
            {
                if (tree.query (pos + 1, i) + tab2 [i].h <= w
                        && tab2 [pos].h + tab2 [i].h <= w)
                    continue;
                flag = false;
                break;
            }
        }
        if (flag)
            printf ("TAK\n");
        else
            printf ("NIE\n");
    }
    return 0;
}