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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <bits/stdc++.h>

using namespace std;

#define SYN_ ios_base::sync_with_stdio(0);cin.tie(0)

#define REP(I,N) for(int I=0;I<(N);I++)
#define FOR(I,A,B) for(int I=(A);I<=(B);I++)
#define SZ(v) (int)((v).size())

#define CLR(ar,val) memset(ar,val,sizeof(ar))

#define PR(x) cout<<(#x)<<" = "<<x<<endl
#define PRLN printf("\n")
#define SHOW(VEC) do{REP(I,SZ(VEC)) cout<<(VEC)[I]<<" "; cout<<endl;}while(0)
#define SHOWARR(ARR,N) do{REP(I,N) cout<<(ARR)[I]<<" "; cout<<endl;}while(0)

//------------------------
#define REPD(I,N) for(int I=(N)-1;I>=0;I--)
#define FORD(I,A,B) for(int I=(A);I>=(B);I--)


#define ALL(X) (X).begin(),(X).end()
#define SRT(A) sort(ALL(A))
#define ERS(VEC,P) VEC.erase(VEC.begin()+P)

#define PB push_back
#define MP make_pair
#define FI first
#define SE second

#define INFTY 2000000000
#define MAXN 1000000000

#define SQR(a)  ((a)*(a))

#define MIN(a,b) ((a)<(b) ? (a):(b))
#define MAX(a,b) ((a)>(b) ? (a):(b))

#define remin(a,b) ((a) = (b) < (a)  ? (b) : (a))
#define remax(a,b) ((a) = (b) > (a)  ? (b) : (a))


#define F_MIN(MIND,ARR,N) do{MIND=0; REP(I,N){ if(ARR[MIND]>ARR[I]){MIND = I;}} }while(0)
#define F_MAX(MIND,ARR,N) do{MIND=0; REP(I,N){ if(ARR[MIND]<ARR[I]){MIND = I;}} }while(0)

#define F_MINV(MIND,VEC) F_MIN(MIND,VEC,SZ(VEC))
#define F_MAXV(MIND,VEC) F_MAX(MIND,VEC,SZ(VEC))

typedef vector<int> VI;
typedef pair<int,int> PII;
typedef long long ll;

typedef list<int> LI;
typedef priority_queue<int> PQI;
typedef vector<PII> VPII;
typedef vector<string> VS;

struct pros
{
    int x1,y1;
    int x2,y2;
    int idx;
    int h;
    int goalIdx;
    friend bool operator<(pros a, pros b)
    {
        if(a.x1 == b.x1)
            return a.x2 < b.x2;
        else
            return a.x1 < b.x1;
    }
};

bool checkifPossible(int n, int w, vector<pros> &beg, vector<pros> &goal)
{
    REP(i,n)
        {
            pros curr = beg[i];
            int goalPos = beg[i].goalIdx;

            if(i<goalPos)
            {//do przodu
                FOR(j,i,goalPos)
                {
                    if((w-beg[j].h)<curr.h && i!=j)
                    {
                        //printf("1 i %d j %d goal %d jh %d\n",i,j,goalPos, beg[j].h );
                        return false;
                    }
                    if((w-goal[j].h)<curr.h && goalPos!=j)
                    {
                        //printf("2 i %d j %d goal %d jh %d\n",i,j,goalPos, goal[j].h );
                        return false;
                    }
                }
            }
            else if(i>goalPos)
            {//cofamy
                FOR(j,goalPos,i)
                {
                    if((w-beg[j].h)<curr.h && i!=j)
                    {
                        //printf("3 i %d j %d goal %d jh %d\n",i,j,goalPos, beg[j].h );
                        return false;
                    }
                    if((w-goal[j].h)<curr.h && goalPos!=j)
                    {
                        //printf("4 i %d j %d goal %d jh %d\n",i,j,goalPos, goal[j].h );
                        return false;
                    }
                }
            }
            else
            {//zostaje w miejscu
                
            }
        }
        return true;
}

inline void swap(vector<pros> &v, int a, int b)
{
    pros c = v[a];
    v[a] = v[b];
    v[b] = c;
}

inline bool canSwap(int w, int ha, int hb)
{
    return w-ha>=hb;
}

bool bubbleTry(int n, int w, vector<pros> &beg)
{
    int newn = 0;
    int newb = 0;
    int nn = n;
    int b = 0;
    bool swapped = false;
    while(b<=nn)
    {
        newn = 0;
        swapped = false;
        FOR(j,b+1,nn-1)
        {
            if(beg[j-1].goalIdx>beg[j].goalIdx && canSwap(w,beg[j-1].h,beg[j].h))
            {
                swap(beg,j-1,j);
                newn=j;
                swapped = true;
            }
        }
        
        nn=newn;
        if(swapped==false)
            break;
        if(beg[newn].goalIdx!=newn){/*printf("1");*/ return false;}
        swapped = false;
        newb=0;
        FORD(j,nn-1,b+1)
        {
            if(beg[j-1].goalIdx>beg[j].goalIdx && canSwap(w,beg[j-1].h,beg[j].h))
            {
                swap(beg,j-1,j);
                newb = j-1;
                swapped = true;
            }
        }
        b=newb;
        if(swapped==false)
            break;
        if(beg[newb].goalIdx!=newb){/*printf("2");*/ return false;}
       
    }
    REP(i,n)
    {
        if(beg[i].goalIdx!=i)
        {
            //printf("%d %d %d\n",beg[i].idx, beg[i].goalIdx, beg[i].h);
            return false;
        }
    }
    return true;
}
int main()
{
    SYN_;
    int t;
    scanf("%d",&t);
    REP(tt,t)
    {
        int n,w;
        scanf("%d %d",&n,&w);
        vector<pros> beg;
        vector<pros> goal;
        beg.resize(n);
        goal.resize(n);
        REP(i,n)
        {
            scanf("%d %d %d %d",&beg[i].x1,&beg[i].y1,&beg[i].x2,&beg[i].y2);
            beg[i].idx = i;
            beg[i].h = abs(beg[i].y2-beg[i].y1);
        }
        REP(i,n)
        {
            scanf("%d %d %d %d",&goal[i].x1,&goal[i].y1,&goal[i].x2,&goal[i].y2);
            goal[i].idx = i;
            goal[i].h = abs(goal[i].y2-goal[i].y1);
        }
        
        
        sort(ALL(goal));
        REP(i,n)
            beg[goal[i].idx].goalIdx = i;
        sort(ALL(beg));
        
        
        /*REP(i,n)
            printf("%d ",beg[i].h);
        printf("\n");
        REP(i,n)
            printf("%d ",beg[i].goalIdx);
        printf("\n");
        REP(i,n)
            printf("%d ",beg[i].idx);
        printf("\n");
        REP(i,n)
            printf("%d ",goal[i].idx);
        printf("\n");*/
        
        if(bubbleTry(n,w,beg))
            printf("TAK\n");
        else
            printf("NIE\n");
        
    }
    
    return 0;
}