program army;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
a:array[1..10000]of integer;
i, j, i1 ,j1, q, b, c, emoe, i2:integer;
begin
assign(input,'army.in');reset (input);
read (c);
for i2:= 1 to c do begin
emoe:=0;
  read (q,b);
  for j:=1 to b  do begin
    for i:=1 to q do begin
        read (a[i]);
    end;
    for i1:=1 to q do begin
      for j1:=1 to q-i1 do begin
        if a[i1]>a[j1+i1] then emoe :=emoe + 1;
      end;
    end;
  end;
  write (emoe);
  writeln;
end;
  { TODO -oUser -cConsole Main : Insert code here }
end.
 