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
//Przemysław Jakub Kozłowski
#include <iostream>
#include <cstdio>
#include <algorithm>
#define FI first
#define SE second
#define MP make_pair
using namespace std;
const int N = 50004, MAXBASE = 65536;

// Drzewo przedziałowe
int base;
int DP[2*MAXBASE+7];
void Init(int n)
{
    base = 1;
    while(base <= n+1) base *= 2;
    for(int i = 1;i <= 2*base;i++) DP[i] = 0;
}
int Query(int a, int b)
{
    a += base;b += base;
    int ret = DP[a];
    if(a != b) ret = max(ret, DP[b]);

    while(a/2 != b/2)
    {
        if(a%2 == 0) ret = max(ret, DP[a+1]);
        if(b%2 == 1) ret = max(ret, DP[b-1]);

        a /= 2;b /= 2;
    }
    return ret;
}
void Set(int a, int c)
{
    a += base;
    DP[a] = c;
    while(a > 1)
    {
        a /= 2;
        DP[a] = max(DP[2*a], DP[2*a+1]);
    }
}
// Koniec Drzewa Przedziałowego

pair<pair<int,int>,pair<int,int> > *gtab;
bool cmp(int a, int b)
{
    return gtab[a].FI.FI < gtab[b].FI.FI;
}

int t;
int n,w;
pair<pair<int,int>,pair<int,int> > taba[N];
pair<pair<int,int>,pair<int,int> > tabb[N];
int kola[N], kolb[N];
int rkolb[N];
int Kb[N];

int main()
{
    scanf("%d", &t);
    for(int ti = 1;ti <= t;ti++)
    {
        scanf("%d%d", &n, &w);
        for(int i = 1;i <= n;i++)
        {
            pair<int,int> p1, p2;
            scanf("%d%d%d%d", &p1.FI, &p1.SE, &p2.FI, &p2.SE);

            pair<int,int> p;
            pair<int,int> r;
            r = MP(abs(p1.FI-p2.FI),abs(p1.SE-p2.SE));
            p.FI = min(p1.FI, p2.FI);
            p.SE = min(p1.SE, p2.SE);

            taba[i] = MP(p, r);
        }
        for(int i = 1;i <= n;i++)
        {
            pair<int,int> p1, p2;
            scanf("%d%d%d%d", &p1.FI, &p1.SE, &p2.FI, &p2.SE);

            pair<int,int> p;
            pair<int,int> r;
            r = MP(abs(p1.FI-p2.FI),abs(p1.SE-p2.SE));
            p.FI = min(p1.FI, p2.FI);
            p.SE = min(p1.SE, p2.SE);

            tabb[i] = MP(p, r);
        }

        /*for(int i = 1;i <= n;i++)
        {
            cerr << "I: " << i << " TABA: " << taba[i].FI.FI << " " << taba[i].FI.SE << " " << taba[i].SE.FI << " " << taba[i].SE.SE << " TABB: " << tabb[i].FI.FI << " " << tabb[i].FI.SE << " " << tabb[i].SE.FI << " " << tabb[i].SE.SE << endl;
        }*/

        bool wyn = true;

        try
        {
            for(int i = 1;i <= n;i++)
                if(taba[i].SE != tabb[i].SE)
                    throw int();

            for(int i = 1;i <= n;i++) kola[i] = kolb[i] = i;
            gtab = taba;
            sort(kola+1,kola+n+1,cmp);
            gtab = tabb;
            sort(kolb+1,kolb+n+1,cmp);

            /*cerr << "kola: ";
            for(int i = 1;i <= n;i++)
                cerr << kola[i] << " ";
            cerr << endl;
            cerr << "kolb: ";
            for(int i = 1;i <= n;i++)
                cerr << kolb[i] << " ";
            cerr << endl;
            cerr << "Ta: ";
            for(int i = 1;i <= n;i++)
                cerr << taba[kola[i]].SE.SE << " ";
            cerr << endl;*/

            Init(n+2);
            for(int i = 1;i <= n;i++) Set(i,taba[kola[i]].SE.SE);
            for(int i = 1;i <= n;i++) rkolb[kolb[i]] = i;
            //for(int i = 1;i <= n;i++) Kb[i] = rkol[kolb[i]];
            //for(int i = 1;i <= n;i++) Kb[kolb[i]] = kola[i];
            for(int i = 1;i <= n;i++) Kb[rkolb[kola[i]]] = i;

            /*cerr << "Kb: ";
            for(int i = 1;i <= n;i++)
                cerr << Kb[i] << " ";
            cerr << endl;*/

            for(int i = n;i >= 1;i--)
            {
                if(Query(Kb[i]+1, n+1)+Query(Kb[i], Kb[i]) > w) throw int();
                Set(Kb[i], 0);
            }
        }
        catch(int)
        {
            wyn = false;
        }

        if(wyn) printf("TAK\n");
        else printf("NIE\n");
    }
    return 0;
}