program ARMY;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
 f:text;
 i,j,c,m,n,s,k:integer;
 mm:array[1..10000]of integer;
procedure sort;
var
 t,y,u:integer;
begin
 for t:= 1 to n do begin
    for y:= t to n do begin
     if mM[t]>mM[y] then begin
      u:=mM[t];
      mM[t]:=mM[y];
      mM[y]:=u;
      s:=s+1;
     end;
    end;
   end; 
end;
begin
 assign(f,'ARMY.IN');
 reset(f);
 readln(f,k);
 s:=0;
 for i:=1 to k do begin
  readln(f,n,m);
  for j:=1 to m do begin
   for c:=1 to n do read(f,mm[c]);
   readln(f);
   sort;
  end;
  writeln(s);
  s:=0;
 end;
 close(f);
end.
 