program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
type mas=array [1..10000,1..10000] of int64;
var M,N,i,oi,pos,q,w,e:integer;
    a:mas;
begin
assign(input,'army.in');
reset(input);
read(q);
for i:=1 to q do
begin oi:=0;
   read(n);
   read(m);
   for w:=1 to m do
   begin
        for e:=1 to n do
        read (a[w,e]);
   end;
   for e:=1 to m do
   begin
   for w:=n downto 2 do
   begin
   pos:=a[e,w];
   If a[e,w]<a[e,w-1] then begin
                             oi:=oi+1;
                             a[e,w]:=a[e,w-1];
                             A[e,w-1]:=pos;
                           end;
   end;
   end;
writeln(oi+1);
end;
end.
 