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
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int S = 1 << 20;

struct tree
{
    //int tab[2*S];
    int tab[2*S];
    void insert( int p, int v )
    {
        p += S;
        tab[p] = v;
        p /= 2;
        while( p != 0 )
        {
            tab[p] = max( tab[2*p], tab[2*p + 1] );
            p /= 2;
        }
    }
    int query( int a, int b )
    {
        a += S - 1;
        b += S + 1;
        int r = 0;
        while( a + 1 < b )
        {
            if( a%2 == 0 ) r = max( r, tab[a+1]);
            if( b%2 == 1 ) r = max( r, tab[b-1]);
            a/=2;
            b/=2;
        }
        return r;
    }
};

struct pa
{
    int x;
    int y;
    int p;
    int p2;
};

bool operator < ( pa a, pa b )
{
    return a.y < b.y;
}

tree Trie;

int main()
{
    int t;
    scanf("%d", &t);
    for( int j = 0; j < t; j ++ )
    {
        int n, w;
        scanf("%d %d", &n, &w);
        //tree Trie;
        pa tab1[n];
        for( int i = 0; i < n; i ++ )
        {
            int x1, y1, x2, y2;
            scanf( "%d %d %d %d", &x1, &y1, &x2, &y2 );
            int h = y2 - y1;
            //Trie.insert( i+1, h );
            tab1[i].x = h;
            tab1[i].y = x1;
            tab1[i].p = i + 1;
            //Trie.insert( i+1, h );
        }
        pa tab2[n];
        for( int i = 0; i < n; i ++ )
        {
            int x1, y1, x2, y2;
            scanf( "%d %d %d %d", &x1, &y1, &x2, &y2 );
            int h = y2 - y1;
            tab2[i].x = h;
            tab2[i].y = x1;
            tab2[i].p = i + 1;
        }
        sort( tab2, tab2 + n );
        for( int i = 0; i < n; i ++ )
        {
            tab1[ tab2[i].p - 1 ].p2 = i + 1;
        }
        sort( tab1, tab1 + n );
        for( int i = 0; i < n; i ++ )
        {
            Trie.insert( i + 1, tab1[i].x );
            tab1[i]. p = i + 1;
        }
        bool flag = true;
        for( int i = 0; i < n; i ++ )
        {
            int H = Trie.query( i + 2, tab1[i].p2 );
            //cout << H + tab1[i].x << " " << tab1[i].p << " " << tab1[i].p2 << endl;
            if( H + tab1[i].x > w && i + 1 != tab1[i].p2 )
            {
                //cout << H + tab1[i].x;
                printf("NIE\n");
                flag = false;
                break;
            }
        }
        if( flag == true ) printf("TAK\n");
    }
    return 0;
}