var f,g:text;
a:array[0..3600,0..3] of longint;
b,best:array[0..3600] of longint;
n,i,j,k,l,t,max:longint;
procedure Sort(l, r: Integer);
var i, j, x, y: integer;
begin
i := l; j := r; x := a[(l+r) DIV 2,3];
repeat
while a[i,3] < x do i := i + 1;
while x < a[j,3] do j := j - 1;
if i <= j then
begin
y := a[i,1]; a[i,1] := a[j,1]; a[j,1] := y;
y := a[i,2]; a[i,2] := a[j,2]; a[j,2] := y;
y := a[i,3]; a[i,3] := a[j,3]; a[j,3] := y;
i := i + 1; j := j - 1;
end;
until i > j;
if l < j then Sort(l, j);
if i < r then Sort(i, r);
end;
begin
assign(f,'cutii.in');
assign(g,'cutii.out');
reset(F);
rewrite(G);
readln(f,n,t);
for k:=1 to t do
begin
for i:=1 to n do
for j:=1 to 3 do
read(f,a[i,j]);
sort(1,n);
b[n]:=1;
for i:=n-1 downto 1 do
begin
max:=0;
j:=i+1;
while (j<n) and (best[j]>max) do
begin
if (a[j,2]>a[i,2]) and (a[j,1]>a[i,1]) then
if b[j]>max then max:=b[j];
inc(j);
end;
b[i]:=1+max;
best[i]:=best[i+1];
if (b[i]>best[i]) then best[i]:=b[i];
end;
max:=0;
for i:=1 to n do
if b[i]>max then max:=b[i];
writeln(g,max);
end;
close(F);
close(G);
end.