program ZJCR;

const maxn = 101;
      maxo   = 40;
      maxins = 1;

type int=longint;

     TLine = array[0..maxn] of int;
     TField = array[1..maxn] of byte;
     TPair = record
       sum,nom:int;
     end;

var
  n,m:int;
  Hor,Ver,H,V:array[1..maxn] of TLine;
  Field,Cur,Best:array[1..maxn] of TField;
  List:array[1..maxn] of TPair;

function GetScore : longint;
var i, j, sc : longint;
 begin
  for i := 1 to n do begin
    H[i][0] := 0;
    j := 1;
    while j <= m do begin
      if Field[i, j] = 2 then begin
        H[i][0] := H[i][0] + 1;
        H[i][H[i][0]] := 0;
        while Field[i, j] = 2 do begin
          H[i][H[i][0]] := H[i][H[i][0]] + 1;
          if j = m then break else j := j + 1;
         end;
       end;
      j := j + 1;
     end;
   end;
  for i := 1 to m do begin
    V[i][0] := 0;
    j := 1;
    while j <= n do begin
      if Field[j, i] = 2 then begin
        V[i][0] := V[i][0] + 1;
        V[i][V[i][0]] := 0;
        while Field[j, i] = 2 do begin
          V[i][V[i][0]] := V[i][V[i][0]] + 1;
          if j = n then break else j := j + 1;
         end;
       end;
      j := j + 1;
     end;
   end;
  sc := 0;
  for i := 1 to n do begin
    if H[i, 0] = Hor[i, 0] then begin
      sc := sc + 1;
      for j := 1 to Hor[i, 0] do if H[i, j] <> Hor[i, j] then begin
        sc := sc - 1;
        break;
       end;
     end;
   end;
  for i := 1 to m do begin
    if V[i, 0] = Ver[i, 0] then begin
      sc := sc + 1;
      for j := 1 to Ver[i, 0] do if V[i, j] <> Ver[i, j] then begin
        sc := sc - 1;
        break;
       end;
     end;
   end;
  GetScore := sc;
 end;

procedure ReadData;
var
  i,ch:int;
begin
  Fillchar(Hor,SizeOf(Hor),0);
  Fillchar(Ver,SizeOf(Ver),0);
  Fillchar(Field,SizeOf(Field),0);
  Fillchar(Cur,SizeOf(Cur),0);
  Read(n,m);
  for i:=1 to n do
  begin
    while true do
    begin
      read(ch);
      if ch<=0 then break;
      inc(Hor[i][0]);
      Hor[i][Hor[i][0]]:=ch;
    end;
  end;
  for i:=1 to m do
  begin
    while true do
    begin
      read(ch);
      if ch<=0 then break;
      inc(Ver[i][0]);
      Ver[i][Ver[i][0]]:=ch;
    end;
  end;
end;

Function SolveLine(m:int; Hor:TLine; var Field:TField):boolean;

// m - Length,  Hor - array of segment's lines, Field - the line from the field

var
  i,j,na,ko,k,l,last:int;
  t:array[0..maxn,0..maxn] of byte;
  black:array[1..maxn] of byte;
  flag,yes:boolean;
  ll:array[1..maxn+1] of int;
  calc:array[1..maxn] of int;
  used:array[1..maxn,1..maxn] of boolean;

  procedure Rec(p,q:int);
  var
    i,na,ko:int;
  begin
    if (p>=Hor[0])or used[p,q] then
    begin
      last:=1;
      if not used[p,q] then ko:=Hor[0]+1 else ko:=p;
      for i:=1 to ko do
      begin
        Fillchar(black[last],sizeof(black[1])*(ll[i]-last),1);
        last:=ll[i]+Hor[i];
      end;
      used[p,q]:=true;
      exit;
    end;
    used[p,q]:=true;
    na:=q+Hor[p]+1;
    ko:=m-Hor[p+1]+1;
    for i:=na to ko do
    begin
      if t[p+1,i] = 2 then
      begin
        ll[p+1]:=i;
        Rec(p+1,i);
      end;
      if Field[i] = 2 then break;
    end;
  end;

begin
  SolveLine:=true;

  Fillchar(calc,sizeof(calc),0);
  calc[m]:=Ord(Field[m]<>1);
  for i:=m-1 downto 1 do
    if Field[i]<>1 then calc[i]:=calc[i+1]+1;

  Fillchar(t,SizeOf(t),0);
  t[0,0]:=1;
  for i:=0 to Hor[0]-1 do
  begin
    if i = 0 then ko:=0 else ko:=m-Hor[i]+1;
    for j:=0 to ko do if t[i,j] = 1 then
    begin
      if i<>0 then na:=j+Hor[i]+1 else na:=1;
      for k:=na to m-Hor[i+1]+1 do
      begin
        flag:=(calc[k]>=Hor[i+1]);
        if flag and (k+Hor[i+1]<=m) and (Field[k+Hor[i+1]]=2) then flag:=false;
        if flag then t[i+1,k]:=1;
        if Field[k] = 2 then break;
      end;
    end;
  end;

  yes:=false;
  for i:=1 to m do if t[Hor[0],i] = 1 then
  begin
    flag:=true;
    for j:=i+Hor[Hor[0]] to m do
      if Field[j] = 2 then
      begin
        flag:=false;
        break;
      end;
    if flag then
    begin
      t[Hor[0],i]:=2;
      yes:=true;
    end;
  end;

  if not yes then
  begin
    SolveLine:=false;
    exit;
  end;

  for i:=Hor[0]-1 downto 1 do
    for j:=1 to m-Hor[i]+1 do if t[i,j] = 1 then
      for k:=j+1+Hor[i] to m-Hor[i+1]+1 do
      begin
        if t[i+1,k] = 2 then
        begin
          t[i,j]:=2;
          break;
        end;
        if Field[k] = 2 then break;
      end;  

  Fillchar(black,SizeOf(black),0);
  for i:=1 to Hor[0] do
    for j:=1 to m-Hor[i]+1 do if t[i,j] = 2 then
      Fillchar(black[j],Sizeof(black[1])*Hor[i],1);
  for i:=1 to m do if black[i] = 0 then Field[i]:=1;

  fillchar(black,sizeof(black),0);
  fillchar(used,sizeof(used),0);
  ll[Hor[0]+1]:=m+1;
  for i:=1 to m do if t[1,i] = 2 then
  begin
    ll[1]:=i;
    Rec(1,i);
  end;

  for i:=1 to m do if black[i] = 0 then Field[i]:=2;
end;

function ConvertHor(nom:int):boolean;
begin
  ConvertHor:=SolveLine(m,Hor[nom],Field[nom]);
end;

function ConvertVer(nom:int):boolean;
var
  f:TField;
  i:int;
begin
  for i:=1 to n do f[i]:=Field[i,nom];
  ConvertVer:=SolveLine(n,Ver[nom],f);
  for i:=1 to n do Field[i,nom]:=f[i];
end;

procedure OutData;
var
  i,j:int;
begin
  for i:=1 to n do
  begin
    for j:=1 to m do if Field[i,j] = 2 then write('#') else write('.');
    writeln; 
  end;
end;

procedure HeapSort(var n:int);
var
  l,r:int; x:TPair;
  procedure sisp(l,r:int);
  var
    i,j:int;
  begin
    i:=l; j:=2*l; x:=List[l];
    if (j<r)and(List[j].sum>List[j+1].sum) then inc(j);
    while (j<=r)and(x.sum>List[j].sum) do
    begin
      List[i]:=List[j];
      i:=j; j:=2*j;
      if (j<r)and(List[j].sum>List[j+1].sum) then inc(j);
    end;
    List[i]:=x;
  end;
begin
  l:=n shr 1 +1; r:=n;
  while l>1 do
  begin
    dec(l); sisp(l,r);
  end;
  l:=1;
  while r>1 do
  begin
    x:=List[l]; List[l]:=List[r]; List[r]:=x;
    dec(r); sisp(l,r);
  end;
end;

procedure SortHor;
var
  i,j,sum:int;
begin
  for i:=1 to n do
  begin
    sum:=0;
    for j:=1 to Hor[i][0] do inc(sum,Hor[i][j]);
    for j:=1 to m do if Field[i,j] = 2 then dec(sum);
    List[i].sum:=sum;
    List[i].nom:=i;
  end;
//  HeapSort(n);
end;

procedure SortVer;
var
  i,j,sum:int;
begin
  for j:=1 to m do
  begin
    sum:=0;
    for i:=1 to Ver[j][0] do inc(sum,Ver[j][i]);
    for i:=1 to n do if Field[i,j] = 2 then dec(sum);
    List[j].sum:=sum;
    List[j].nom:=j;
  end;
//  HeapSort(m);
end;

procedure SortHH;
var
  i,j,sum,y:int;
begin
  y := maxins;
  for i:=n downto 1 do
  begin
    sum:=0;
    for j:=1 to Hor[i][0] do inc(sum,Hor[i][j]);
    for j:=1 to m do if Field[i,j] = 2 then dec(sum);
    List[i].sum:=sum;
    List[i].nom:=i;
    if sum > 0 then begin
      for j := m downto 1 do 
       if Field[i,j]=0 then begin
         Field[i,j] := 2;
         y := y - 1;
         if y = 0 then exit;
         sum := sum - 1;
         if sum = 0 then break;
        end;
     end;
  end;
//  HeapSort(n);
end;

procedure Solve;
var
  i,j,bst,k,t,o:int;
  done,change:boolean;
begin
  bst := 0;
  o := 0;
  while true do
  begin

    done:=true;
    Cur:=Field;

    SortHor;
    for i:=1 to n do
      ConvertHor(List[i].nom);
    SortVer;
    for i:=1 to m do
      ConvertVer(List[i].nom);

    change:=false;
    for i:=1 to n do
      for j:=1 to m do
      begin
        if Field[i,j] = 0 then done:=false;
        if Cur[i,j]<>Field[i,j] then Change:=true;
      end;

    j := GetScore;
    if j > bst then begin
      bst := j;
      Best := Field;
     end;

    if done then break;
    if not change then begin
      o := o + 1;
      if o > maxo then break;
      SortHH;
     end;

  end;
 Field := Best;

 if (bst < n) or (bst < m) then begin
   FillChar(Field, Sizeof(Field), 0);
   if n > m then begin
     for i := 1 to n do begin
       k := 0;
       for j := 1 to Hor[i, 0] do begin
         for t := 1 to Hor[i, j] do begin
           k := k + 1;
           Field[i, k] := 2;
          end;
         k := k + 1;
        end;
      end;
    end else begin
     for i := 1 to m do begin
       k := 0;
       for j := 1 to Ver[i, 0] do begin
         for t := 1 to Ver[i, j] do begin
           k := k + 1;
           Field[k, i] := 2;
          end;
         k := k + 1;
        end;
      end;
    end;
  end;
end;

var t, ti : longint;

begin
 read(t);
 for ti := 1 to t do begin
   ReadData;
   Solve;
   OutData;
  end;
end.