var
 a:array[1..10000] of integer;
 m,n,i,j2,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;
  for i:=1 to n do
   read(a[i]);
  for i:=1 to n-1 do begin
   for j2:=i+1 to n do begin
    if a[j2]<a[i] then begin
     q:=a[i]; a[i]:=a[j2]; a[j2]:=q;
      k:=k+1;
   end; end; end;
   y:=y+k;
 end;
  writeln(y);
 end;
end.
