#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
inline ll read(){
ll x=0;char ch=getchar();bool f=0;
for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=1;
for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return f?-x:x;
}
void write(ll x){
if (x<0) putchar('-'),x=-x;
if (x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);putchar('\n');}
void writep(ll x){write(x);putchar(' ');}
#define int long long
int const N=2e5+3;
int t,n,m,l;
struct node{
int l,r;
}a[N],b[N];
bool cmp(node A,node B){
return max(A.l,A.r)<max(B.l,B.r);
}
signed main(){
t=read();
while (t--){
n=read();l=read();m=0;
for (int i=1;i<=n;i++){
b[i].l=read();b[i].r=read();
if (b[i].l!=-1 || b[i].r!=-1) a[++m].l=b[i].l,a[m].r=b[i].r;
}
sort(a+1,a+m+1,cmp);
int pos=1,flag=0,tot=l,Min=0;
a[0].r=0;
for (int i=1;i<=m;i++){
if (a[i].l!=-1 && (a[i].l==a[i-1].l || a[i].l==a[i-1].r)){
flag=1;
break;
}
if (a[i].r!=-1 && (a[i].r==a[i-1].l || a[i].r==a[i-1].r)){
flag=1;
break;
}
if (a[i-1].r!=-1 && a[i].l!=-1 && a[i-1].r+1<a[i].l) Min++;
if (a[i].l!=-1 && pos>a[i].l){
flag=1;
break;
}
if (a[i].l!=-1 && a[i].r!=-1) tot-=(a[i].r-a[i].l+1);
else tot--;
if (tot<n-m || n-m<Min){
flag=1;
break;
}
pos=max(a[i].l,a[i].r)+1;
}
if (m>0 && a[m].r!=-1 && a[m].r!=l) Min++;
if (flag || tot<n-m || n-m<Min) puts("NIE");
else puts("TAK");
}
}
/*
3
4 51
1 -1
11 50
-1 -1
-1 10
3 2
-1 -1
-1 -1
-1 -1
2 3
1 2
2 3
*