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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<tr1/unordered_map>
#include<queue>
#include<cstdlib>
#include<list>
#include<set>
#include<map>
#include<cmath>
#define MP make_pair
#define PB push_back
#define s second
#define f first
#define PII pair<int,int>
#define VPII vector <pair<int,int> >
#define VI vector <int>
#define abs(a) max((a),-(a))
#define LL long long
#define LD long double
#define ALL(x) x.begin(),x.end()
#define PU putchar_unlocked
#define GU getchar_unlocked
#define DBG(x) cerr<<#x<<" "<<(x)<<endl;
using namespace std;
int a,b,c,d,e,f,n,m,mx,l,r,k,z,h;
int wynik;
char ch;
int INF=1e9+1;
const int MXN=5e4+4;
int x1,x2,y11,y22;
int ktory[MXN];
int t[MXN];
const int BASE=65536;
int x[BASE*2];
void insert(int a,int co)
{
a+=BASE;
while(a)
    {
    x[a]=max(x[a],co);
    a/=2;
    }
}
int query(int a,int b)
{
int ret=0;
a+=BASE-1;
b+=BASE+1;
while(a/2!=b/2)
    {
    if(a%2==0)ret=max(ret,x[a+1]);
    if(b%2==1)ret=max(ret,x[b-1]);
    a/=2;
    b/=2;
    }
return ret;
}
void solve()
{
scanf("%d%d",&n,&h);
VPII V1,V2;
for(int i=1;i<=n+BASE;i++)x[i]=0;


for(int i=1;i<=n;i++)
    {
    scanf("%d%d%d%d",&x1,&y11,&x2,&y22);
    if(x1>x2)swap(x1,x2);
    if(y11>y22)swap(y11,y22);
    t[i]=y22-y11;
    V1.PB(MP(x1,i));
    }
sort(ALL(V1));

for(int i=1;i<=n;i++)
    {
    scanf("%d%d%d%d",&x1,&y11,&x2,&y22);
    if(x1>x2)swap(x1,x2);
    if(y11>y22)swap(y11,y22);
    V2.PB(MP(x1,i));
    }
sort(ALL(V2));
for(int i=0;i<V2.size();i++)
    {
    ktory[V2[i].s]=i;
    }
for(int i=0;i<V1.size();i++)
    {
    a=ktory[V1[i].s];
    if(h-query(a,n)<t[V1[i].s])
        {
        puts("NIE");
        return;
        }
    insert(a,t[V1[i].s]);
    }
puts("TAK");
}
main()
{
scanf("%d",&z);
while(z--)solve();
}