program IKEYB;
var i,j,j2,j3,t,k,l,odzadu: Integer;
    Suma,MinSuma: LongInt;
    s,Klice,Pismo:string[90];
    Pole,Vysl,f:array[1..90] of Integer;
    
procedure SpoctiSumu;
begin
  Suma:=0;j3:=1;j2:=1;  
  For j:=1 to l do begin
    Inc(Suma,j2*f[j]);
    if j2<Pole[j3] then Inc(j2) else begin
      J2:=1; Inc(j3);
    end;
  end;
  if Suma<MinSuma then begin
    Vysl:=Pole;  
    MinSuma:=Suma;
  end;
end;
    
begin
  ReadLn(t);
  for i:=1 to t do begin
    MinSuma:=2000000000;
    ReadLn(k,l);ReadLn(Klice);ReadLn(Pismo);
    For j:=1 to l do ReadLn(f[j]);
    For j:=1 to k-1 do Pole[j]:=1;Pole[k]:=l-k+1;        
    SpoctiSumu;
    Odzadu:=k;
    while Odzadu>1 do begin
      Inc(Pole[Odzadu-1]);
      if Pole[Odzadu]=2 then begin
        Pole[Odzadu]:=1;     
        Dec(Odzadu);
      end
      else begin
        Pole[k]:=Pole[Odzadu]-1;
        if Odzadu<k then begin
          Pole[Odzadu]:=1;     
          Odzadu:=k;
        end;  
      end;
      SpoctiSumu;
    end;
    WriteLn("Keypad #",i,":");
    j2:=1;
    For j:=1 to k do begin
      Write(Klice[j],": ");
      For j3:=j2 to j2+Vysl[j]-1 do Write(Pismo[j3]);
      WriteLn;
      Inc(j2,Vysl[j]);
    end;  
  end;
end.    
    
