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
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#define s(x); scanf("%d",&x);
using namespace std;
int cars, height;
vector <int> h,special,tree;
priority_queue <pair<int,int > > QS,QK;

struct four{
  int a,b,c,d;
};


void fix(int k){
  while(k>=1){
    k = k>>1;
    tree[k] = max(tree[k*2],tree[(k*2)+1]);
  }
  return;
}

void change(int x, int k){
  tree[x+(1<<16)] = max(tree[x+(1<<16)],k);
  fix(x+(1<<16));
}

int maximal(int x, int y, int p, int k, int w){
  if(x==p&&y==k) return tree[w];
  if(x >= ((p+k)>>1)) return maximal(x,y,((p+k)>>1),k,(w*2)+1);
  else if( y < ((p+k)>>1)) return maximal(x,y,p,((p+k)>>1),w*2);
  else return max(maximal(x,((p+k)>>1),p,((p+k)>>1),(w*2)),maximal(((p+k)>>1),y,((p+k)>>1),k,(w*2)+1));
}


void czysc(){
  cars = 0;
  height = 0;
  h.clear();
  special.clear();
  tree.clear();
  tree.resize(1<<17,0);
  while(!QS.empty()) QS.pop();
  while(!QK.empty()) QK.pop();
  return;
}

void read_start(){
  for(int i = 0; i < cars; i++){
      four k;
      s(k.a);
      s(k.b);
      s(k.c);
      s(k.d);
      h[i] = abs(k.b-k.d);
      QS.push(make_pair(-max(k.a,k.c),i));
    }
  return;
}

void read_end(){
  for(int i = 0; i < cars; i++){
      four k;
      s(k.a);
      s(k.b);
      s(k.c);
      s(k.d);
      QK.push(make_pair(-max(k.a,k.c),i));
    }
  return;
}

void star(){
  for(int i = 0; i < cars; i++){
    int a;
    a = QK.top().second;
    QK.pop();
    special[a] = i;
  }
  return;
}

bool ending(){
  while(!QS.empty()){
      int a = QS.top().second;
      QS.pop();
      int mini = maximal(special[a]+(1<<16),1<<17,1<<16,1<<17,1) + h[a];
      if(mini>height)
	return false;
      else
	change(special[a],h[a]);
  }
  return true;
  
}

void double_star(bool k){
  if(k) printf("TAK\n");
  else printf("NIE\n");
  return;
}

void solve(){
    czysc();
    s(cars); s(height);
	h.resize(cars+1,0);
	special.resize(cars+1,0);
    read_start();
    read_end();
    star();
    double_star(ending());
    return;
}


int main(){
  int t;
  s(t);
  while(t--)
    solve();
  return 0;
}