var
 a:array[1..10000] of integer;
 m,n,i,j,k,q,y,i2,n2:integer;
 ok:boolean;
begin
 assign(input,'ARMY.IN'); reset(input);
 read(n2);
 for i2:=1 to n2 do begin
 read(n,m);  y:=0;
 for j:=1 to m do begin
  k:=0;  ok:=false;
  for i:=1 to n do
   read(a[i]);
  while (ok=false) do begin
  for i:=1 to n-1 do begin
   if a[i]>a[i+1] then  begin
    q:=a[i]; a[i]:=a[i+1]; a[i+1]:=q;
     k:=k+1;
   end;
  end; ok:=true;
  for i:=1 to n-1 do begin
   if a[i]>a[i+1] then begin
   ok:=false; break;
    end; end;
  end;
   y:=y+k;
 end;
  writeln(y);
 end;
end.
