program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var M,N,i,oi,q,w,e,z:integer;
    a:array [1..10000,1..100] of integer;
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:=m downto 1 do
   for w:=n downto 2 do
   begin
   for z:=n downto 2 do
   If a[e,w]<a[e,z-1] then oi:=oi+1;
   end;
writeln(oi);
end;
end.
