program Project1;

{$APPTYPE CONSOLE}
{
function Move(mv:boolean; step:byte; start:byte; len:byte):Byte; //0-D, 1-U
begin
   step:=step mod len;
   if step=0 then step:=len;
   if mv then
   begin
      result:=(start+step) mod len;
      if result=0 then result:=len;
   end
   else
   begin
      result:=(start-step);
      if result<1 then result:=result+len;
   end;
end;   }

type
   TArr= array of integer;
   TArray = array of TArr;
   TLongNumber = array of Word;

var
   f:textfile;
   ar:TArray;
   T:byte;
   H,L,i,k,j,b:byte;
   Summ:TLongNumber;
   ex:boolean;
 //  lvlar:TLvlAr;
   find:boolean;
   lvl,ind:byte;
   res1,res2:TLongNumber;
   st:string;
   dx:word;

procedure InputNumber(const st:string; out ar:TLongNumber);
var
   i,k:word;
   er:integer;
begin
   k:=Length(st) div 3;
   if Length(st) mod 3>0 then Inc(k);
   SetLength(ar,k);
   if k>1 then
      for i:=1 to k-1 do
         Val(copy(st,Length(st)-i*3+1,3),ar[i-1],er);
   Val(copy(st,1,Length(st)-(k-1)*3),ar[high(ar)],er);
end;

procedure OutputNumber(const ar:TLongNumber; out st:string);
var
   s:string;
   i,k:word;
begin
   st:='';
   for i:=0 to high(ar) do
   begin
      Str(ar[i],s);
      if Length(s)<3 then
         for k:=1 to 3-Length(s) do
            s:='0'+s;
      st:=s+st;
   end;
   for i:=1 to Length(st) do
      if st[i]<>'0' then
      begin
         if i=1 then break;
         st:=copy(st,i,Length(st));
         break;
      end;
end;

procedure Addition(var Ar1,Ar2:TLongNumber; out Res:TLongNumber);
var
   i,k,d:word;
begin
   if Length(Ar1)>=Length(Ar2) then
      k:=Length(Ar1)
   else
      k:=Length(Ar2);
   if Length(Ar1)<k then SetLength(ar1,k);
   if Length(Ar2)<k then SetLength(ar2,k);
   d:=0;
   SetLength(Res,k);
   for i:=0 to k-1 do
   begin
      Res[i]:=ar1[i]+ar2[i]+d;
      d:=res[i] div 1000;
      res[i]:=res[i] mod 1000;
   end;
   if d>0 then
   begin
      if Length(res)<k+1 then
         SetLength(res,Length(res)+1);
      res[k]:=res[k]+d;
   end;
end;

procedure Multiplication(var Ar1,Ar2:TLongNumber; out res:TLongNumber);
var
   d,i,k:word;
   c:cardinal;
begin
   SetLength(res,High(ar1)+High(ar2)+1);
   d:=0;
   for i:=0 to high(ar1) do
   begin
      for k:=0 to high(ar2) do
      begin
         c:=ar1[i]*ar2[k];
         res[i+k]:=res[i+k]+c mod 1000 + d;
         d:=c div 1000 + res[i+k] div 1000;
         res[i+k]:=res[i+k] mod 1000;
      end;
      if d>0 then
      begin
         if Length(res)<i+high(ar2)+2 then
            SetLength(res,Length(res)+1);
         res[i+high(ar2)+1]:=res[i+high(ar2)+1]+d;
      end;
   end;
end;

procedure Move(var ar:TArr; step:byte; left:boolean);
var
   i:byte;
   d:TArr;
begin
   if step=0 then exit;
   if left then
   begin
      SetLength(d,step);
      for i:=0 to step-1 do
         d[i]:=ar[i];
      for i:=step to high(ar) do
         ar[i-step]:=ar[i];
      for i:=0 to high(d) do
         ar[Length(ar)-step+i]:=d[i];
   end
   else
   begin
      SetLength(d,step);
      for i:=0 to step-1 do
         d[i]:=ar[Length(ar)-step+i];
      for i:=Length(ar)-step-1 downto 0 do
         ar[i+step]:=ar[i];
      for i:=0 to high(d) do
         ar[i]:=d[i];
   end;
end;

procedure Calculate(lev:byte;var ar:TArr; ind:byte; out res:TLongNumber);
var
   k:byte;
   res1,res2,res3,res4:TLongNumber;
   st:string;
begin
   if Length(ar)-ind<ind then
   begin
      k:=Length(ar)-ind;
      Move(ar,k,false);
   end
   else
   begin
      k:=ind;
      Move(ar,ind,true);
   end;
   str(lev,st);
   InputNumber(st,res1);
   InputNumber('20',res2);
   Multiplication(res1,res2,res3);
   str(k,st);
   InputNumber(st,res1);
   InputNumber('5',res2);
   Multiplication(res1,res2,res4);
   Addition(res3,res4,res);
end;

begin
   AssignFile(f,'TOWER.IN');
   Reset(f);
   readln(f,T);
   for i:=1 to T do
   begin
      SetLength(summ,0);
      readln(f,h,l);
      SetLength(Ar,h);
      for k:=0 to high(ar) do
      begin
         SetLength(ar[k],l);
         if l>1 then
            for j:=0 to high(ar[k])-1 do
               read(f,ar[k][j]);
         readln(f,ar[k][high(ar[k])]);
      end;
      for dx:=1 to h*l do
      begin
         ex:=false;
         find:=false;
         for b:=0 to high(ar) do
         begin
            for j:=0 to high(ar[b]) do
               if ar[b][j]=dx then
               begin
                  lvl:=b;
                  ind:=j;
                  ex:=true;
                  find:=true;
                  break;
               end;
            if ex then  break;
         end;
         if find then
         begin
            Calculate(lvl,ar[lvl],ind,res2);
            Addition(res2,Summ,res1);
            Summ:=res1;
         end;
         if not find then break;
      end;
      OutputNumber(Summ,st);
      writeln(st);
   end;
   Close(f);
end.
