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
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <map>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <cstring>
#define REP(i,x,v)for(int i=x;i<=v;i++)
#define REPD(i,x,v)for(int i=x;i>=v;i--)
#define FOR(i,v)for(int i=0;i<v;i++)
#define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define FOREACH(i,t) FORE(i,t)
#define REMIN(x,y) (x)=min((x),(y))
#define REMAX(x,y) (x)=max((x),(y))
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define IN(x,y) ((y).find((x))!=(y).end())
#define un(v) v.erase(unique(ALL(v)),v.end())
#define LOLDBG
#ifdef LOLDBG
#define DBG(vari) cerr<<#vari<<" = "<<vari<<endl;
#define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl;
#else
#define DBG(vari)
#define DBG2(v1,v2)
#endif
#define CZ(x) scanf("%d",&(x));
#define CZ2(x,y) scanf("%d%d",&(x),&(y));
#define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z));
#define ALL(x) (x).begin(),(x).end()
#define tests int dsdsf;cin>>dsdsf;while(dsdsf--)
#define testss int dsdsf;CZ(dsdsf);while(dsdsf--)
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; }

int preh[111111];
int h[111111];
int rev[111111];
int n,w;

bool check(vi p)
{
    FOR(i,n)FOR(j,i)
    {
        if (p[j]>p[i] && h[p[j]]+h[p[i]]>w) return 0;
    }
    return 1;
}

bool ans;
int mxl[111111];

vi msort(vi p)
{
    int d=p.sz;
    if (d==1) return p;
    vi l,r;
    FOR(i,d) if (i<d/2) l.pb(p[i]); else r.pb(p[i]);
    l=msort(l);
    r=msort(r);
    int ld=l.sz,rd=r.sz;
    mxl[ld]=0;REPD(i,ld-1,0) mxl[i]=max(mxl[i+1],h[l[i]]);
    p.clear();
    int i1=0,i2=0;
    while(p.sz<d)
	{
        if (i1==ld) {p.pb(r[i2++]);continue;}
        if (i2==rd) {p.pb(l[i1++]);continue;}
        if (l[i1]<r[i2]) p.pb(l[i1++]);
        else
		{
            if (mxl[i1]+h[r[i2]]>w) ans=0;
            p.pb(r[i2++]);
		}
	}
	return p;
}

bool fast_check(vi p)
{
    ans=1;
    msort(p);
    return ans;
}


int main()
{
	/*int cnt=0;
    while(1)
	{
		cnt++;
		if (cnt%10000==0) DBG(cnt);
        n=1+rand()%30;
        w=1+rand()%20;
        FOR(i,n)h[i]=1+rand()%20;
        vi p;FOR(i,n)p.pb(i);
        random_shuffle(ALL(p));
        //DBG(mp(fast_check(p),check(p)));
        if (fast_check(p)!=check(p))
		{
			DBG(fast_check(p));
			DBG(check(p));
			DBG("ROTFL");
			return 0;
		}
	}*/
    testss
    {

        CZ2(n,w);
        vector<pii> v;
        FOR(i,n)
        {
            int a,b,c,d;
            CZ2(a,b);CZ2(c,d);
            if (a>c) swap(a,c);if (b>d) swap(b,d);
            preh[i]=d-b;
            v.pb(mp(a,i));
        }
        sort(ALL(v));
        FOR(i,n) rev[v[i].se]=i;
        FOR(i,n) h[i]=preh[v[i].se];
        v.clear();
        FOR(i,n)
        {
			int a,b,c,d;
            CZ2(a,b);CZ2(c,d);
            if (a>c) swap(a,c);if (b>d) swap(b,d);
            v.pb(mp(a,rev[i]));
        }
		sort(ALL(v));
		vi per;
		FOR(i,n)per.pb(v[i].se);
        if (fast_check(per)) puts("TAK");
        else puts("NIE");

    }

    return 0;
}