#include <stdio.h>

char jap[120][120];
int horz[120][60];
int vert[120][60];
int hN[120];
int vN[120];
int C,R;

#ifdef ONLINE_JUDGE
#define __int64 long long
#endif	

class MyInt {
	__int64 hi,lo;
public:
	void operator&=(MyInt x) {hi&=x.hi; lo&=x.lo;}
	void operator|=(MyInt x) {hi|=x.hi; lo|=x.lo;}
	bool operator!=(MyInt& x) {return (!((hi==x.hi) && (lo==x.lo))); }
	bool operator==(MyInt& x) {return ((hi==x.hi) && (lo==x.lo)); }
	bool operator!=(long i) {return (!((hi==(i>=0)?0:(-1)) && (lo==i))); }
	bool operator==(long i) {return ((hi==(i>=0)?0:(-1)) && (lo==i)); }

	void operator=(long i) {if (i<0) {hi=-1; lo=i;} else {hi=0; lo=i;} }
	void operator<<=(int i) {
		if (i==0) return;
		if (i>0) {hi<<=i; hi|=(lo>>(64-i)); lo<<=i;	} else {i=-i; lo>>=i; lo|=(hi<<(64-i));}
	}
	void operator>>=(int i) {
		if (i==0) return;
		if (i>0) {lo>>=i; lo|=(hi<<(64-i));} else {i=-i; hi<<=i; hi|=(lo>>(64-i)); lo<<=i;}
	}
	void operator--(){
		if (lo) lo--; else {lo--; hi--;}
	}

	MyInt operator~() {MyInt ret; ret.hi=~hi; ret.lo=~lo; return ret;}
	MyInt operator&(MyInt& x) { MyInt ret; ret=*this; ret&=x; return ret;}
	MyInt operator|(MyInt& x) { MyInt ret; ret=*this; ret|=x; return ret;}
};

//#define LLInt __int64
#define LLInt MyInt
#define i64 __int64

class MyType {
public:
	LLInt mand;
	LLInt mor;
	long num;
};
class MyType2 {
public:
	i64 mand;
	i64 mor;
	long num;
};
MyType table[120][120];
MyType2 table2[120][120];
int Km[120];
int Sm[120];
int NK;
struct MyState {
	LLInt rowAnd[120], rowOr[120], colAnd[120], colOr[120];
	int chRow[120], chCol[120];
	int change;
	int dead;
	double q;
	long badC, badR, what;
} state;
struct MyState2 {
	i64 rowAnd[120], rowOr[120], colAnd[120], colOr[120];
	int chRow[120], chCol[120];
	int change;
	int dead;
	double q;
	long badC, badR, what;
} state2;
LLInt maskAnd, maskOr;
i64 maskAnd2, maskOr2;

bool check(LLInt mand, LLInt mor)
{
	if ((mor&maskAnd)!=maskAnd) return false;
	if ((mand|maskOr)!=maskOr) return false;
	return true;
}
bool check(i64 mand, i64 mor)
{
	if ((mor&maskAnd2)!=maskAnd2) return false;
	if ((mand|maskOr2)!=maskOr2) return false;
	return true;
}
void ClearTable(long n, long m)
{
	long i,j;
	for (i=n; i>=0; i--) for (j=m; j>=0; j--) table[i][j].num=-1;
}
void ClearTable2(long n, long m)
{
	long i,j;
	for (i=n; i>=0; i--) for (j=m; j>=0; j--) table2[i][j].num=-1;
}

void Recursive(long n, long m)
{
	MyType *ret=&table[n][m], *tout;
	if (ret->num!=-1) return;
	ret->num=0; ret->mand=-1; ret->mor=0;
	if (m==0) {
		ret->mand=0; 
		ret->mor=-1;
		ret->mor<<=n;
		if (check(ret->mand, ret->mor)) ret->num=1; else ret->num=0;
		return;
	} else {
		if (Sm[m]+m-1>n) return;
		LLInt mand, mor;
		mand=1; mand<<=Km[m]; --mand; mand<<=n-Km[m];
		mor=1; mor<<=n-Km[m]-1; mor=~mor;
		long i,n1;
		LLInt unit; unit=1; unit<<=n-1; unit=~unit; 
		for (i=0; i<=n-(Sm[m]+m-1); (i++), (mand>>=1), (mor>>=1), (mor&=unit)) {
			if (!check(mand,mor)) continue;
			n1=n-Km[m]-i-1;
			if (n1>0) {
				Recursive(n1,m-1);
				tout=&table[n1][m-1];
				if (tout->num==0) continue;
				ret->mand&=(mand|tout->mand);
				ret->mor|=(mor&tout->mor);
				ret->num++;//=tout->num;
			} else {
				ret->num++;
				ret->mand&=mand;
				ret->mor|=mor;
			}
			//mand>>=1; mor>>=1;
		}
	}
}
void Recursive2(long n, long m)
{
	MyType2 *ret=&table2[n][m], *tout;
	if (ret->num!=-1) return;
	ret->num=0; ret->mand=-1; ret->mor=0;
	if (m==0) {
		ret->mand=0; 
		ret->mor=-1;
		ret->mor<<=n;
		if (check(ret->mand, ret->mor)) ret->num=1; else ret->num=0;
		return;
	} else {
		if (Sm[m]+m-1>n) return;
		i64 mand, mor;
		mand=1; mand<<=Km[m]; --mand; mand<<=n-Km[m];
		mor=1; mor<<=n-Km[m]-1; mor=~mor;
		long i,n1;
		i64 unit; unit=1; unit<<=n-1; unit=~unit; 
		for (i=0; i<=n-(Sm[m]+m-1); (i++), (mand>>=1), (mor>>=1), (mor&=unit)) {
			if (!check(mand,mor)) continue;
			n1=n-Km[m]-i-1;
			if (n1>0) {
				Recursive2(n1,m-1);
				tout=&table2[n1][m-1];
				if (tout->num==0) continue;
				ret->mand&=(mand|tout->mand);
				ret->mor|=(mor&tout->mor);
				ret->num++;//=tout->num;
			} else {
				ret->num++;
				ret->mand&=mand;
				ret->mor|=mor;
			}
			//mand>>=1; mor>>=1;
		}
	}
}
void ChangeRowState(long rIndex)
{
	if (state.chRow[rIndex]==0) return;
	state.chRow[rIndex]=0;
	if (state.rowAnd[rIndex]==state.rowOr[rIndex]) return;
	Sm[0]=0;
	long i;
	for (i=hN[rIndex]-1; i>=0; i--) {
		Km[hN[rIndex]-i]=horz[rIndex][i];
		Sm[hN[rIndex]-i]=Sm[hN[rIndex]-i-1]+Km[hN[rIndex]-i];
	}
	maskAnd=state.rowAnd[rIndex]; maskOr=state.rowOr[rIndex];
	ClearTable(C,hN[rIndex]);
	Recursive(C,hN[rIndex]);
	if (table[C][hN[rIndex]].num==0) state.dead=1;
	if (state.rowAnd[rIndex]!=table[C][hN[rIndex]].mand) {state.change=1;}
	if (state.rowOr[rIndex]!=table[C][hN[rIndex]].mor) {state.change=1;}

	state.rowAnd[rIndex]=table[C][hN[rIndex]].mand;
	state.rowOr[rIndex]=table[C][hN[rIndex]].mor;
	LLInt mand,mor, cur;
	mand=1; mand<<=(R-rIndex-1);
	mor=1; mor<<=(R-rIndex-1); mor=~mor;
	cur=1;
	for (i=C-1; i>=0; i--) {
		if ((state.rowAnd[rIndex]&cur)!=0) {
			if ((state.colAnd[i]&mand)==0) state.chCol[i]=1;
			state.colAnd[i]|=mand;
		}
		if ((state.rowOr[rIndex]&cur)==0) {
			if ((state.colOr[i]&mand)!=0) state.chCol[i]=1;
			state.colOr[i]&=mor;
		}
		cur<<=1;
	}
}
void ChangeRowState2(long rIndex)
{
	if (state2.chRow[rIndex]==0) return;
	state2.chRow[rIndex]=0;
	if (state2.rowAnd[rIndex]==state2.rowOr[rIndex]) return;
	Sm[0]=0;
	long i;
	for (i=hN[rIndex]-1; i>=0; i--) {
		Km[hN[rIndex]-i]=horz[rIndex][i];
		Sm[hN[rIndex]-i]=Sm[hN[rIndex]-i-1]+Km[hN[rIndex]-i];
	}
	maskAnd2=state2.rowAnd[rIndex]; maskOr2=state2.rowOr[rIndex];
	ClearTable2(C,hN[rIndex]);
	Recursive2(C,hN[rIndex]);
	if (table2[C][hN[rIndex]].num==0) state2.dead=1;
	if (state2.rowAnd[rIndex]!=table2[C][hN[rIndex]].mand) {state2.change=1;}
	if (state2.rowOr[rIndex]!=table2[C][hN[rIndex]].mor) {state2.change=1;}
	
	state2.rowAnd[rIndex]=table2[C][hN[rIndex]].mand;
	state2.rowOr[rIndex]=table2[C][hN[rIndex]].mor;
	i64 mand,mor, cur;
	mand=1; mand<<=(R-rIndex-1);
	mor=1; mor<<=(R-rIndex-1); mor=~mor;
	cur=1;
	for (i=C-1; i>=0; i--) {
		if ((state2.rowAnd[rIndex]&cur)!=0) {
			if ((state2.colAnd[i]&mand)==0) state2.chCol[i]=1;
			state2.colAnd[i]|=mand;
		}
		if ((state2.rowOr[rIndex]&cur)==0) {
			if ((state2.colOr[i]&mand)!=0) state2.chCol[i]=1;
			state2.colOr[i]&=mor;
		}
		cur<<=1;
	}
}
void ChangeColState(long cIndex)
{
	if (state.chCol[cIndex]==0) return;
	if (state.colAnd[cIndex]==state.colOr[cIndex]) return;
	Sm[0]=0;
	long i;
	for (i=vN[cIndex]-1; i>=0; i--) {
		Km[vN[cIndex]-i]=vert[cIndex][i];
		Sm[vN[cIndex]-i]=Sm[vN[cIndex]-i-1]+Km[vN[cIndex]-i];
	}
	maskAnd=state.colAnd[cIndex]; maskOr=state.colOr[cIndex];
	ClearTable(R,vN[cIndex]);
	Recursive(R,vN[cIndex]);
	if (table[R][vN[cIndex]].num==0) state.dead=1;
	if (state.colAnd[cIndex]!=table[R][vN[cIndex]].mand) {state.change=1; }
	if (state.colOr[cIndex]!=table[R][vN[cIndex]].mor) {state.change=1; }

	state.colAnd[cIndex]=table[R][vN[cIndex]].mand;
	state.colOr[cIndex]=table[R][vN[cIndex]].mor;
	LLInt mand,mor, cur;
	mand=1; mand<<=(C-cIndex-1);
	mor=1; mor<<=(C-cIndex-1); mor=~mor;
	cur=1;
	for (i=R-1; i>=0; i--) {
		if ((state.colAnd[cIndex]&cur)!=0) {
			if ((state.rowAnd[i]&mand)==0) state.chRow[i]=1;
			state.rowAnd[i]|=mand;
		}
		if ((state.colOr[cIndex]&cur)==0) {
			if ((state.rowOr[i]&mand)!=0) state.chRow[i]=1;
			state.rowOr[i]&=mor;
		}
		cur<<=1;
	}
}
void ChangeColState2(long cIndex)
{
	if (state2.chCol[cIndex]==0) return;
	if (state2.colAnd[cIndex]==state2.colOr[cIndex]) return;
	Sm[0]=0;
	long i;
	for (i=vN[cIndex]-1; i>=0; i--) {
		Km[vN[cIndex]-i]=vert[cIndex][i];
		Sm[vN[cIndex]-i]=Sm[vN[cIndex]-i-1]+Km[vN[cIndex]-i];
	}
	maskAnd2=state2.colAnd[cIndex]; maskOr2=state2.colOr[cIndex];
	ClearTable2(R,vN[cIndex]);
	Recursive2(R,vN[cIndex]);
	if (table2[R][vN[cIndex]].num==0) state2.dead=1;
	if (state2.colAnd[cIndex]!=table2[R][vN[cIndex]].mand) {state2.change=1; }
	if (state2.colOr[cIndex]!=table2[R][vN[cIndex]].mor) {state2.change=1; }
	
	state2.colAnd[cIndex]=table2[R][vN[cIndex]].mand;
	state2.colOr[cIndex]=table2[R][vN[cIndex]].mor;
	i64 mand,mor, cur;
	mand=1; mand<<=(C-cIndex-1);
	mor=1; mor<<=(C-cIndex-1); mor=~mor;
	cur=1;
	for (i=R-1; i>=0; i--) {
		if ((state2.colAnd[cIndex]&cur)!=0) {
			if ((state2.rowAnd[i]&mand)==0) state2.chRow[i]=1;
			state2.rowAnd[i]|=mand;
		}
		if ((state2.colOr[cIndex]&cur)==0) {
			if ((state2.rowOr[i]&mand)!=0) state2.chRow[i]=1;
			state2.rowOr[i]&=mor;
		}
		cur<<=1;
	}
}


void ClearState()
{
	long i;
	for (i=0; i<120; i++){
		state.colAnd[i]=0; state.colOr[i]=-1;
		state.rowAnd[i]=0; state.rowOr[i]=-1;
		state.chRow[i]=1; state.chCol[i]=1;
	}
	state.change=0; state.dead=0;
}

void ClearState2()
{
	long i;
	for (i=0; i<120; i++){
		state2.colAnd[i]=0; state2.colOr[i]=-1;
		state2.rowAnd[i]=0; state2.rowOr[i]=-1;
		state2.chRow[i]=1; state2.chCol[i]=1;
	}
	state2.change=0; state2.dead=0;
}

long badR,badC;
double GetQuality();
int Finish()
{
	int ret=0;
	LLInt cur;
	long i,j;
	for (j=0; j<R; j++) {
		cur=1;
		for (i=C-1; i>=0; i--) {
			jap[j][i]='?';
			if ((state.rowAnd[j]&cur)!=0) jap[j][i]='#';
			if ((state.rowOr[j]&cur)==0) { if (jap[j][i]=='#') ret|=2; jap[j][i]='.';}
			if (jap[j][i]=='?') {ret|=1;jap[j][i]='.'; state.badR=badR=j; state.badC=badC=i;}
			cur<<=1;
		}
	}
	state.q=GetQuality();
	return ret;
}
int Finish2()
{
	int ret=0;
	i64 cur;
	long i,j;
	for (j=0; j<R; j++) {
		cur=1;
		for (i=C-1; i>=0; i--) {
			jap[j][i]='?';
			if ((state2.rowAnd[j]&cur)!=0) jap[j][i]='#';
			if ((state2.rowOr[j]&cur)==0) { if (jap[j][i]=='#') ret|=2; jap[j][i]='.';}
			if (jap[j][i]=='?') {ret|=1;jap[j][i]='.'; state2.badR=badR=j; state2.badC=badC=i;}
			cur<<=1;
		}
	}
	state2.q=GetQuality();
	return ret;
}
void SetX()
{
	LLInt cur;
	cur=1; cur<<=R-badR-1;	state.colAnd[badC]|=cur; state.chCol[badC]=1;
	cur=1; cur<<=C-badC-1;	state.rowAnd[badR]|=cur; state.chRow[badR]=1;
	state.what=1;
}
void SetO()
{
	LLInt cur;
	cur=1; cur<<=R-badR-1;	state.colOr[badC]&=~cur; state.chCol[badC]=1;
	cur=1; cur<<=C-badC-1;	state.rowOr[badR]&=~cur; state.chRow[badR]=1;
	state.what=0;
}
void SetX2()
{
	i64 cur;
	cur=1; cur<<=R-badR-1;	state2.colAnd[badC]|=cur; state2.chCol[badC]=1;
	cur=1; cur<<=C-badC-1;	state2.rowAnd[badR]|=cur; state2.chRow[badR]=1;
	state2.what=1;
}
void SetO2()
{
	i64 cur;
	cur=1; cur<<=R-badR-1;	state2.colOr[badC]&=~cur; state2.chCol[badC]=1;
	cur=1; cur<<=C-badC-1;	state2.rowOr[badR]&=~cur; state2.chRow[badR]=1;
	state2.what=0;
}
double qMax;
MyState best;

int Process()
{
	MyState *sts=new MyState[C*R+1];
	long index=0;
	int ret=0;
	ClearState();
	long i,j;
	qMax=-1;
	for (i=0; i<C*R+1; i++) {
		state.change=0;
		for (j=0; j<R; j++) {
			ChangeRowState(j);
		}
		for (j=0; j<C; j++) {
			ChangeColState(j);
		}
		if (state.dead) {
			break;
			Finish();
			if (state.q>qMax) {best=state; qMax=state.q;}
			if (index==0) break;
			while (index) {
				index--;
				state=sts[index];
				if (state.what==1) {
					badC=state.badC; badR=state.badR;
					SetO();
					sts[index]=state;
					index++;
					break;
				}
			}
			if (index==0) break;
			//printf("Dead\n\n");
		} else {
			if (state.change==0) {
				ret=Finish();
				if ((ret&1)==0) break;
				SetX();
				//sts[index]=state; index++;
				//if (state.q>qMax) {best=state; qMax=state.q;}
			}
		}
	}
#ifndef ONLINE_JUDGE
	printf("%i\n\n",i);	
#endif	
	ret=Finish();
	if (qMax>state.q) { state=best; ret=Finish();}

	delete sts;

	return ret;
}

MyState2 best2;

int Process2()
{
	MyState2 *sts=new MyState2[C*R+1];
	long index=0;
	int ret=0;
	ClearState2();
	long i,j;
	qMax=-1;
	for (i=0; i<C*R+1; i++) {
		state2.change=0;
		for (j=0; j<R; j++) {
			ChangeRowState2(j);
		}
		for (j=0; j<C; j++) {
			ChangeColState2(j);
		}
		if (state2.dead) {
			Finish2();
			if (state2.q>qMax) {best2=state2;qMax=state2.q;}
			if (index==0) break;
			while (index) {
				index--;
				state2=sts[index];
				if (state2.what==0) {
					badC=state2.badC; badR=state2.badR;
					SetX2();
					sts[index]=state2;
					index++;
					break;
				}
			}
			if (index==0) break;
			//printf("Dead\n\n");
		} else {
			if (state2.change==0) {
				ret=Finish2();
				if ((ret&1)==0) break;
				if (state2.q>qMax) {best2=state2;qMax=state2.q;}
				state2.what=0;
				sts[index]=state2; index++;
				SetO2();
			}
		}
	}
#ifndef ONLINE_JUDGE
	printf("%i\n\n",i);	
#endif
	
	ret=Finish2();
	if (qMax>state2.q) { state2=best2; ret=Finish2();}
	
	delete sts;
	return ret;
}

void FillCell(int type, int i, int j, int k)
{
	if (type) {
		jap[j][i]=k?'#':'.';
	} else {
		jap[i][j]=k?'#':'.';
	}
}

void Fill(int type, int helps[][60], int *NH, long M)
{
	int j,k,i,index;
	for (j=0; j<M; j++) {
		index=0;		
		for (k=0; k<NH[j]; k++) {
			for (i=0; i<helps[j][k]; i++) {
				FillCell(type,index,j,1);
				index++;
			}
			index++;
		}
	}
}

void ClearOut()
{
	int j,k;
	for (j=0; j<R; j++) {
		for (k=0; k<C; k++) jap[j][k]='.';
		jap[j][k]=0;
	}	
	for (k=0; k<C; k++) jap[R][k]=0;
}

double GetQuality()
{
	int j,k, index, count, flagLine, flagAll=1;
	int ret=0;
	for (j=0; j<R; j++) {
		index=0;
		flagLine=1;
		for (k=0; k<C; k++) {
			if (jap[j][k]=='.') continue;
			count=0;
			while (jap[j][k]=='#') {
				count++; k++;
			}
			if (horz[j][index]!=count) {
				flagAll=0; flagLine=0;
				break;
			} else {
				k--;
				index++;
			}
		}
		if ((index==hN[j]) && (flagLine)) ret++; else flagAll=0;
	}

	for (k=0; k<C; k++) {
		index=0;
		flagLine=1;
		for (j=0; j<R; j++) {
			if (jap[j][k]=='.') continue;
			count=0;
			while (jap[j][k]=='#') {
				count++; j++;
			}
			if (vert[k][index]!=count) {
				flagAll=0; flagLine=0;
				break;
			} else {
				j--;
				index++;
			}
		}
		if ((index==vN[k]) && (flagLine)) ret++; else flagAll=0;
	}
	return (flagAll)?(1.5*(double)ret):((double)ret);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
#endif	
	
	double q1,q2;

	int i,n,j,k;
	int type;
	scanf("%i",&n);
	for (i=0; i<n; i++) {
		scanf("%i %i",&R,&C);
		for (j=0; j<R; j++) {
			for (k=0; ;k++) {
				scanf("%i",&(horz[j][k]));
				if (horz[j][k]==0) break;
			}			
			hN[j]=k;
		}		
		for (j=0; j<C; j++) {
			for (k=0; ;k++) {
				scanf("%i",&(vert[j][k]));
				if (vert[j][k]==0) break;
			}			
			vN[j]=k;
		}
		type=(R>C)?1:0;
		ClearOut();
		Fill(type,type?horz:vert,type?hN:vN,type?R:C);
		q1=GetQuality();

		if ((R<600) && (C<600)) {
			ClearOut();
			if ((R<62) && (C<62))  Process2(); else Process();
			//if (ret==2) return 1;
			q2=GetQuality();
			if (q2<q1) {				
				ClearOut();
				Fill(type,type?horz:vert,type?hN:vN,type?R:C);
			}
		}
		for (j=0; j<R; j++) {
			//for (k=0; k<C; k++) printf("%c",jap[j][k]);printf("\n");
			printf(jap[j]); printf("\n");
		}
		
	}
	return 0;
}