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
program lus;

type
  tab = array[1..100000, 1..2] of longword;
  tab1 = array[1..400000] of longword;

var
  m : byte;
  n : longword;
  a1,a2,a3,a4 : array[1..100000, 1..2] of longword;
  b : array[1..400000] of longword;
  i,j,k : longword;
  k1 : byte;
  tmp : longword;


procedure qsort(var t:tab; l,r:longword);
var
  p,tmp : longword;
  i,j : longword;
  begin
    i:=l; j:=r;
    tmp:=(l+r) div 2;
    p:=t[tmp,1];
    repeat
      while t[i,1]<p do i:=i+1;
      while p<t[j,1] do j:=j-1;
      if i<=j then
      begin
        tmp:=t[i,1]; t[i,1]:=t[j,1]; t[j,1]:=tmp;
        tmp:=t[i,2]; t[i,2]:=t[j,2]; t[j,2]:=tmp;
        i:=i+1; j:=j-1
      end
    until i>=j;
    if l<j then qsort(t,l,j);
    if i<r then qsort(t,i,r)
  end;
procedure qsort1(var t:tab1; l,r:longword);
var
  p,tmp : longword;
  i,j : longword;
  begin
    i:=l; j:=r;
    tmp:=(l+r) div 2;
    p:=t[tmp];
    repeat
      while t[i]<p do i:=i+1;
      while p<t[j] do j:=j-1;
      if i<=j then
      begin
        tmp:=t[i]; t[i]:=t[j]; t[j]:=tmp;
        i:=i+1; j:=j-1
      end
    until i>=j;
    if l<j then qsort1(t,l,j);
    if i<r then qsort1(t,i,r)
  end;

begin
  readln(m);

  for i:=1 to m do
  begin
    readln(n);
    for j:=1 to n do
    begin
      read(a1[j,1]); read(a2[j,1]); read(a3[j,1]); readln(a4[j,1]);
      a1[j,2]:=j; a2[j,2]:=j; a3[j,2]:=j; a4[j,2]:=j;
    end;
    qsort(a1,1,n); qsort(a2,1,n); qsort(a3,1,n); qsort(a4,1,n);
    tmp:=a1[1,1]; k:=1; b[k]:=a1[1,2];
    j:=2;
    while (j<=n) and (a1[j,1]=tmp) do
    begin
      k:=k+1; b[k]:=a1[j,2]; j:=j+1
    end;
    tmp:=a3[1,1]; k:=k+1; b[k]:=a3[1,2];
    j:=2;
    while (j<=n) and (a3[j,1]=tmp) do
    begin
      k:=k+1; b[k]:=a3[j,2]; j:=j+1
    end;
    tmp:=a2[n,1]; k:=k+1; b[k]:=a2[n,2];
    j:=n-1;
    while (j>0) and (a2[j,1]=tmp) do
    begin
      k:=k+1; b[k]:=a2[j,2]; j:=j-1
    end;
    tmp:=a4[n,1]; k:=k+1; b[k]:=a4[n,2];
    j:=n-1;
    while (j>0) and (a4[j,1]=tmp) do
    begin
      k:=k+1; b[k]:=a4[j,2]; j:=j-1
    end;
    qsort1(b,1,k);
    tmp:=b[1]; k1:=1; j:=2;
    repeat
      if b[j]=tmp then k1:=k1+1
      else
      begin
        tmp:=b[j]; k1:=1
      end;
      j:=j+1
    until (j>k) or (k1=4);
    if k1=4 then writeln('TAK')
        else writeln('NIE')
  end;
end.