program army;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var x,j,y,z,i,q,w:integer;
m:array[1..10000] of integer;
f: text;
begin
assign(f,'army.in');
reset(f);
q:=0;
read(f,z);
repeat begin
read(f,x,y);
for w:=1 to y do begin
for i:=1 to x do begin
read(f,m[i]);
end;
for j:=1 to x do begin
for i:=j to x do begin
if m[j]>m[i] then q:=q+1;
end;
end;
end;
writeln(q);
q:=0;
end;
until eof(f);
  { TODO -oUser -cConsole Main : Insert code here }
end.
