{$GOTO ON}
const
  act_mode = false;
  steplimit  = 15000;
  blklimit   = 5;
  {}
  max_blocks = 50;
  max_rows   = 50;
  max_cols  = 100;
  max_rc    = 100; (*  max(max_rows,max_cols)*)
  max_total = max_rows+max_cols;
  {}
  trailsize = 200000;

const
  fil = '#'; clr = '.'; unc = '?';
type
  int=longint;

var
  blklim: int;
  stepcount:  int;
  limitex:boolean;
  flag2: int;

  try_count,iks_count: int;
{}
  rowcnt, colcnt: int;
  pmap:  array [0..max_rows, 0..max_cols] of char;

  b_cnt: array [0..max_total] of int;
  b_len: array [0..max_total,0..max_blocks] of int;

  b_slv: array [0..max_total] of byte;
  b_stt: array [0..max_total] of byte; (* 0/1/2*)

  bno,ll,np: int;

{}

procedure read_test;
  function readline(i: int): int;
    var j,n: int;
    begin
      j := 0;
      repeat read(n); b_len[i,j] := n; inc(j) until n = 0;
      readln;
      exit(j-1);
    end;
  var  i, j: int;
  begin
    ll := 1;
    bno := 0;
    readln(rowcnt, colcnt);
    for i := 0 to rowcnt-1 do begin
      j := readline(bno);
      b_cnt[bno] := j;
      inc(bno);
      inc(ll);
    end;
    for i := 0 to colcnt-1 do begin
      j := readline(bno);
      b_cnt[bno] := j;
      inc(bno);
      inc(ll);
    end;

  end;

procedure write_res;
  var i,j: int;
  begin
    for i := 0 to rowcnt-1 do begin
      for j := 0 to colcnt-1 do
        if pmap[i,j]= unc then write(clr) else write(pmap[i,j]);
      writeln;
    end;
  end;

function pm_get(i: int): char;
  begin
    if bno<rowcnt then pm_get := pmap[bno,i] else pm_get := pmap[i,bno-rowcnt];
  end;

procedure pm_set(i: int;  c: char);
  begin
    if bno<rowcnt then  pmap[bno,i] := c  else pmap[i,bno-rowcnt] := c;
  end;

var trail: array [0..trailsize] of int;
    trindex: int;
    btflg : boolean;

procedure totrail(i: int);
  begin
    inc(trindex); trail[trindex] := bno;
    inc(trindex); trail[trindex] := i;
  end;

procedure backtrack(n: int);
  var i: int;
  begin
    while trindex>n do begin
      i := trail[trindex]; dec(trindex);
      bno := trail[trindex]; dec(trindex);
      pm_set(i,unc);

      dec(b_slv[bno]);
      if bno<rowcnt then  dec(b_slv[rowcnt+i])  else  dec(b_slv[i]);
    end;
  end;

procedure trythis(i: int;  c: char);
  begin
    inc(try_count);
    if btflg then totrail(i);
    if bno<rowcnt then begin
      pmap[bno,i] := c;
      b_stt[i+rowcnt] := 2;
      inc(b_slv[i+rowcnt])
      end
    else begin
      pmap[i,bno-rowcnt] := c;
      b_stt[i] := 2;
      inc(b_slv[i])
    end;
    inc(b_slv[bno]);
  end;


var
  line: array [0..max_rc] of char;
  vbl,vml,bml: array  [0..max_blocks] of int;
  p:  array   [0..max_blocks] of int;
  pr: array   [0..max_blocks] of int;

(**)

function prop_pr(i: int;  j: int): boolean;
  var k,opj,oopj: int;
  label next,prev;

  function blkmove(j: int; i: int): int;
    var  opj, oopj: int;
    label next;
    begin
      if j<0 then begin blkmove := -1; exit; end;

      opj := p[j];
      i := i-vbl[j];
      oopj := i+1;

    next:
      p[j] := i+1;
      i := p[j]+vbl[j];
      if i > ll then begin blkmove := -1; exit;    end;

      dec(i);
      while  i>=p[j] do begin
        if line[i]=clr then  goto next ;
        dec(i);
      end;

      i := p[j]+vbl[j];

      if vml[j] <> 0 then
        while (i<ll) and (line[i]=fil) do begin
          inc(i);
          inc(p[j]);
        end;

      if (bml[j] <> 0) and (p[j]>oopj) then begin
        blkmove := blkmove(j-1,p[j]-1);
        exit;
      end;

      for  i := p[j]-1 downto opj do
        if line[i]=fil then begin
          blkmove := blkmove(j-1,i);
          exit;
        end;

      blkmove := j;
    end;

  begin
    if i <> 0 then begin
      for k := i-1 downto p[j] do
        if line[k]=fil then begin
          j := blkmove(j-1,k);
          goto prev;
        end;
    end;

    while true do begin
      oopj := i;

    next:
      p[j] := i;
      i := p[j]+vbl[j];
      if i>ll then begin prop_pr := false; exit; end;

      if j=(np-1) then
        for k := ll-1 downto i do
          if line[k]=fil then begin
            j := blkmove(j,k);
            goto prev;
          end;

      dec(i);
      while i>=p[j] do begin
        if line[i]=clr then begin
          for k := i-1 downto p[j] do
            if line[k]=fil then begin
              j := blkmove(j-1,k);
              goto prev;
            end;
          inc(i);
          goto next;
        end;
        dec(i);
      end;

      opj := p[j];
      i := opj+vbl[j];
      if vml[j] <> 0{} then
      while (i<ll) and (line[i]=fil) do begin
        inc(i);
        inc(p[j]);
      end;

      if (bml[j] <> 0{}) and (p[j]>oopj) then begin
        j := blkmove(j-1,p[j]-1);
        goto prev;
      end;

      for  i := p[j]-1 downto opj do
        if line[i]=fil then begin
          j := blkmove(j-1,i);
          break;
        end;
    prev:
      if j<0 then begin exit(false); end;
      if j=(np-1) then begin exit(true); end;
      i := p[j]+vbl[j]+vml[j];
      inc(j);
    end;
  end;

function mvplrt: boolean;
  var  i, j: int;
  begin
    np := b_cnt[bno];
    for  j := 0 to np-1 do begin
      vbl[j] := b_len[bno,(np-1)-j];
      if j<(np-1) then  vml[j] := 1 else vml[j] := 0;
      bml[j] := 0;
    end;
    for  i := 0 to ll-1 do line[i] := pm_get(ll-1-i);

    if not prop_pr(0,0) then begin exit(false);   end;

    for  j := 0 to np-1 do begin
      vbl[j] := b_len[bno,j];
      if j<(np-1) then vml[j] := 1 else
      vml[j] := 0;
      bml[j] := 0;
      pr[j] := ll-p[(np-1)-j]-vbl[j];
    end;

    for  i := 0 to ll-1 do line[i] := pm_get(i);
    {}
    exit(prop_pr(0,0));
  end;


//
var
    pom: array [0..max_blocks] of int;
    pp:  array [0..max_rc] of byte;
    lind: array[0..max_rc] of int;

procedure modeint;
  var i,j,j0,j1,k,k0,k1,pre_i: int;
      pre_fl:boolean;
  begin
    k := 0; j0 := 0; j1 := 0; k0 := 0; k1 := 0;
    for  i := 0 to ll-1 do begin
      pp[i] := 1;
      if (k0 <> 0) and (p[k0-1]+vbl[k0-1]=i) then
      j0 := 0;
      if (k0 <> np) and (p[k0]=i) then begin
        inc(k0);
        k := k0;
        j0 := 1;
      end;
      if (k1 <> 0) and (pr[k1-1]+vbl[k1-1]=i) then
      j1 := 0;
      if (k1 <> np) and (pr[k1]=i) then begin
        inc(k1);
        j1 := 1;
      end;
      if (j0=j1) and (pm_get(i)=unc) then begin
        pp[i] := 3+j0;
        lind[i] := k-1;
      end;
    end;
    for  i := 0 to np-1 do pom[i] := p[i];

    for  i := (ll-1) downto 0 do begin
      if pp[i]>2 then begin
        if lind[i]>-1 then begin
          if pp[i]=3 then begin
            line[i] := fil;
            pre_fl := prop_pr(i-b_len[bno,lind[i]]+1,lind[i]);
          end
          else begin
            line[i] := clr;
            pre_fl := prop_pr(i+1,lind[i]);
          end;
        end
        else begin
          pre_fl := false;
        end;
        line[i] := unc;
        if pre_fl then begin
          if pp[i]=3 then begin
            if i-b_len[bno,lind[i]]+1=p[lind[i]] then begin
              if lind[i]=0 then pre_i := -1 else pre_i := p[lind[i]-1]+vbl[lind[i]-1];
              for  j := i-1 downto succ(pre_i) do begin
                if pm_get(j) <> unc then break;
                if pp[j]=3 then pp[j] := 1;
              end;
            end;
          end
          else begin
            if lind[i]=0 then pre_i := -1 else pre_i := p[lind[i]-1]+vbl[lind[i]-1];
            for  j := i-1 downto succ(pre_i) do begin
              if pp[j]=4 then pp[j] := 1;
            end;
          end;
        end;
        if not pre_fl then begin
          if pp[i]=4 then trythis(i,fil) else trythis(i,clr);
        end;
        for  j := 0 to np-1 do p[j] := pom[j];
      end;
    end;
  end;

function psol1: boolean;

  function psol_line: boolean;
    var  i,j: int;

    function chk_line: boolean;
      var  i,j: int;
      begin
        np := b_cnt[bno];
        if np = 0 then begin exit(true) end
        else begin
          for j := 0 to np-1 do begin
            vbl[j] := b_len[bno,j];
            vml[j] := 0;
            bml[j] := 0;
            if (j>0) and (vml[j-1]<0) then begin
              vml[j-1] := 0;
              bml[j] := 1;
            end;
          end;
          for i := 0 to ll-1 do line[i] := pm_get(i);
          exit(prop_pr(0,0));
        end;
      end;

  begin
      if(b_stt[bno]=0) or ((b_stt[bno]=1) and (flag2=0)) then begin
        exit( true);
      end;

      if b_slv[bno]=ll then begin
        if not chk_line then begin exit(false) end;
        b_stt[bno] := 0;
        exit( true);
      end;

      if b_cnt[bno]> blklim then begin
        inc(iks_count);
        exit(true);
      end;

      if b_cnt[bno]=0 then begin
        for i := 0 to ll-1 do line[i] := pm_get(i);
        for i := 0 to ll-1 do if line[i]=unc then trythis(i,clr);
        exit( true);
      end;

      if not mvplrt then begin
        exit(false);
      end;

      if flag2 <> 0 then begin
        if (b_stt[bno]=1) and (not act_mode) then begin
          i := 0;
          while (i<ll) and (pm_get(i)<> unc) do inc(i);
          while (i<ll) and (pm_get(i) = unc) do inc(i);
          while (i<ll) and (pm_get(i)<> unc) do inc(i);
          if i=ll then begin
            b_stt[bno] := 0;
            exit(true);
          end;
        end;
        modeint;
        end
      else begin
        {tryrl}
        for  j := 0 to np-1 do
          for  i := pr[j] to p[j]+vbl[j]-1 do
            if line[i]=unc then trythis(i,fil);

        for  i := 0 to p[0]-1 do
          if line[i]=unc then trythis(i,clr);

        for  i := pr[np-1]+vbl[np-1] to ll-1 do
          if line[i]=unc then trythis(i,clr);

        for  j := 0 to np-1-1 do
          for  i := pr[j]+vbl[j] to p[j+1]-1 do
            if line[i]=unc then trythis(i,clr);
      end;

      if flag2 <> 0 then b_stt[bno] := 0 else b_stt[bno] := 1;
      exit( true);
    end;

  begin
    while true do begin
      if flag2 <> 0 then dec(flag2);
      if act_mode then flag2 := 1;
      iks_count := 0;

      ll := colcnt;
      for bno := 0 to rowcnt-1 do
        if not psol_line then begin exit(false) end;

      ll := rowcnt;
      for  bno := rowcnt to rowcnt+colcnt-1 do
        if not psol_line then begin exit( false); end;

      if try_count = 0 then begin
        if iks_count = 0 then begin
          if flag2 <> 0 then begin exit(true) end else flag2 := 2
        end
        else if blklim < max_blocks then begin
          blklim:= max_blocks
        end
        else if flag2 <> 0 then begin
          exit(true)
        end
        else
          flag2 := 2
      end
      else begin
        try_count := 0; inc(stepcount)
      end
    end
  end;

  procedure solve;
    var backindex, i: int;
    begin
      if limitex then begin exit end;

      if not psol1 then begin exit end;
      ll := colcnt;

      for bno := 0 to rowcnt-1 do if b_slv[bno] <> ll then break;

      if bno = rowcnt-1 then begin
        limitex:=true;
        exit;
        end
      else if stepcount > steplimit then begin
        limitex:=true;
        exit;
        end
      else begin
        for  i := 0 to ll-1 do if pm_get(i)=unc then  break;
        backindex := trindex;
        btflg := true;

        trythis(i,clr);
        b_stt[bno] := 2;
        solve; if limitex then begin exit end;
        backtrack(backindex);

        trythis(i,fil);
        b_stt[bno] := 2;
        solve; if limitex then begin exit end;
        backtrack(backindex);
      end;
    end;

{}

procedure do_test;
  var i, j: int;
  begin
    {init}
    for  i := 0 to rowcnt-1 do
      for j := 0 to colcnt-1 do  pmap[i,j] := unc;

    ll := colcnt;
    for bno := 0 to rowcnt+colcnt-1 do begin
      b_stt[bno] := 0;
      b_slv[bno] := 0;
    end;
    {}
    trindex := 0;
    btflg := false;
    blklim:= blklimit ;
    flag2 := 0;
    limitex:=false;
    stepcount := 0;
    try_count := 0;

    solve;
    write_res;
  end;

procedure  main;
var testcount, testno:int;
  begin
    readln(testcount);
    for testno := 1 to testcount do begin
      read_test;
      do_test;
    end;
  end;

begin
  main;
end.