#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
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){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

const int max_size=100000;
char buf[max_size];
int buf_size=0,pos=0;

#define getbuf() buf_size=fread(buf,1,max_size,stdin)
#define get(tok) {if (pos>=buf_size) { if (feof(stdin)) tok=0;	else { getbuf();pos=0; tok=buf[pos++];}}else tok=buf[pos++];}
#define in(v) {int tok;get(tok);while (tok==' ' || tok=='\n')get(tok);v=tok-'0';get(tok);while(tok>='0' && tok<='9'){v*=10;v+=(tok-'0');get(tok);}}

struct MaxTree {
   int* el, s;
   MaxTree (int h) { // dziedzina elementow to [0..2^h-1]
      el = new int[2*(s = 1<<h)]; FOR(x,2*s) el[x] = -INF;
   }
   void Set (int p, int v) { // uwaga: może overwritować mniejszą wartość
      for (p += s, el[p] = v, p /= 2; p > 0; p /= 2) el[p] = max(el[2*p],el[2*p+1]);
   }
   int Find (int p, int k) { // wyznacz min na [p,k]
      int m = -INF; p += s; k += s;
      while (p < k) {
         if (p&1)    m = max(m, el[p++]);
         if (!(k&1)) m = max(m, el[k--]);
         p /= 2; k /= 2;
      }
      if (p == k) m = max(m, el[p]);
      return m;
   }
};
// kod z ksiazki Stanczyka


#define N 100007
int sx1[N], sy1[N], sx2[N], sy2[N], ex1[N], ey1[N], ex2[N], ey2[N], h[N], posE[N];

bool testcase () {
   int n, H;
   in(n); in(H);
   FOR(i,n) {
      in(sx1[i]);
      in(sy1[i]);
      in(sx2[i]);
      in(sy2[i]);
      if (sx1[i] > sx2[i]) swap(sx1[i], sx2[i]);
      if (sy1[i] > sy2[i]) swap(sy1[i], sy2[i]);
   }
   FOR(i,n) {
      in(ex1[i]);
      in(ey1[i]);
      in(ex2[i]);
      in(ey2[i]);
      if (ex1[i] > ex2[i]) swap(ex1[i], ex2[i]);
      if (ey1[i] > ey2[i]) swap(ey1[i], ey2[i]);
   }

   // na wszelki wypadek
   FOR(i,n) {
      if (sx2[i] - sx1[i] != ex2[i] - ex1[i] || sy2[i] - sy1[i] != ey2[i] - ey1[i]) {
         return false;
      }
      h[i] = sy2[i] - sy1[i];
   }

   vector<pair<int,int> > sorterS, sorterE;
   FOR(i,n) {
      sorterS.pb(mp(sx1[i], i));
      sorterE.pb(mp(ex1[i], i));
   }
   sort(ALL(sorterS));
   sort(ALL(sorterE));
   FOR(k,n) posE[sorterE[k].se] = k;

   int p = 2;
   while ((1<<p) <= n) ++p;
   MaxTree tr(p);

   FOREACH(it,sorterS) {
      const int i = it->se;
      int maks = tr.Find(posE[i], n-1);
      if (maks + h[i] > H) return false;
      tr.Set(posE[i], h[i]);
   }

   return true;
}

int main () {
   int tests; in(tests);
   while (tests--) {
      printf(testcase() ? "TAK\n" : "NIE\n");
   }
}
