uses
  SysUtils;
var
a:array[1..10000] of integer;
kol,n,m,s,i1,i2,i3,i:integer;
begin
assign(input,'army.in'); reset(input);
read(kol);
for i:=1 to kol do
  begin
    s:=0;
    read(n,m);
    for i1:=1 to m do
      begin
        for i2:=1 to n do read(a[i]);
        for i2:=1 to n do
          begin
            if a[i2]>a[i2+1] then
            begin
              for i3:=i2 to n do if a[i2]>a[i3] then s:=s+1;
            end;
          end;
      end;
      writeln(s);
  end;
end.
