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
#include <iostream>
#include <algorithm>

#define MAX 50001

using namespace std;

struct Car {
  int nc;
  int x;
  int w;
};

Car P1[MAX];
Car P2[MAX];

int n,w, w_2;

bool CompareX(const Car& a, const Car& b)
{
  if (a.x != b.x)
    return a.x<b.x;
  else
    return a.w<b.w;
};

bool CompareW(const Car& a, const Car& b)
{
  if(a.w != b.w)
    return a.w<b.w;
  else
    return a.nc<b.nc;
};

void allSort(Car P[MAX])
{
  int start, end, mov;
  Car tmp;
  end = n;
  start = n;
  
  while (start>0)
  {
    --start;    
    while(start>0 && P[start].w<=w_2)
    {
      --start;
    }
    
    if(P[start].w<=w_2) 
    {
      sort(P+start, P+end, CompareW);
      break;
    }
    else if (start+1<end){
      sort(P+start+1, P+end, CompareW);
    }
    
    if (P[start].w>w_2)
    {
      mov = start;
      while(mov<n-1 && P[mov].w+P[mov+1].w<=w)
      {
        tmp = P[mov];
        P[mov] = P[mov+1];
        P[mov+1] = tmp;
        ++mov;
      }
      end = mov;
    }
  }
}


int main()
{
  ios_base::sync_with_stdio(0);

  int t;
  int x1, y1, x2, y2;
  int yes;
  
  cin >> t;

  for(int i=0; i<t; ++i)
  {
    cin >> n >> w;
    
    yes = 1;
    w_2 = w/2;
    
    for(int j=0; j<n; ++j)
    {
      cin >> x1 >> y1 >> x2 >> y2;
      
      P1[j].nc = j;
      P1[j].x = min(x1, x2);
      P1[j].w = abs(y1-y2);
      if (P1[j].w > w_2)
        yes = 0;
    }
    
    for(int j=0; j<n; ++j)
    {
      cin >> x1 >> y1 >> x2 >> y2;
      
      P2[j].nc = j;
      P2[j].x = min(x1, x2);
      P2[j].w = abs(y1-y2);
    }
    
    if (yes)
    {
      cout << "TAK\n";
      continue;
    }
    
    sort(P1, P1+n, CompareX);
    allSort(P1);

    sort(P2, P2+n, CompareX);
    allSort(P2);
   
    int j;
    for(j=1; j<n; ++j)
    {
      if(P1[j].nc != P2[j].nc)
      {
        cout << "NIE\n";
        break;
      }
    }
    if(j==n)
      cout << "TAK\n";
  }
  
  return 0;
}