#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

typedef unsigned long long ll;

int N;
int blH,blW;
int bdH,bdW;
int block[4][101][101];
int field[101][101];

ll bk[4][101];
ll bd[101];
ll bits[65];

void RotateBlock(){
    for(int k=0;k<3;k++)for(int j=0;j<blW;j++)for(int i=0;i<blH;i++)
        block[k+1][j][blH-1-i]=block[k][i][j];
    for(int i=0;i<4;i++){
        for(int j=0;j<blH;j++){
            bk[i][j]=0;
            for(int k=0;k<blW;k++)if(block[i][j][k])bk[i][j]|=(1ULL<<k);
        }
    }
}

int main(){
    bits[1]=1;
    for(int i=2;i<=64;i++)bits[i]=(bits[i-1]<<1)+1;
    cin>>N;
    int prv=-2;
    while(N--){
        memset(block,0,sizeof(block));
        cin>>blH>>blW;
        for(int i=0;i<blH;i++){
            for(int j=0;j<blW;j++){
                char ch;
                cin>>ch;
                if(ch=='#')block[0][i][j]=1;
                else block[0][i][j]=0;
            }
        }
        blH=max(blH,blW);
        blW=blH;
        cin>>bdH>>bdW;
        for(int i=0;i<bdH;i++){
            bd[i]=0;
            for(int j=0;j<bdW;j++){
                char ch;
                cin>>ch;
                if(ch=='#'){
                    field[i][j]=1;
                    bd[i]|=(1ULL<<j);
                }
                else field[i][j]=0;
            }
        }
        // ubNðñ]³¹ÂÂSêð·
        // ubNÌEºÌÀWª{[hãÌÇÌÀWÉêv·é©H
        // ñ]
        RotateBlock();
        int res=-1;
        for(int k=0;k<4;k++){
            for(int i=0;i<bdH+blH-1;i++){
                for(int j=0;j<bdW+blW-1;j++){
                    int cnt=0;
                    // ubNªÍÝoéàµ­ÍdÈè`FbN
                    bool isSetable=true;
                    for(int l=0;l<blH;l++){
                        int bdy=-blH+l+1+i;
                        if(bdy<0||bdy>=bdH){
                            if(bk[k][l]!=0){
                                isSetable=false;
                                break;
                            }
                        }
                        // ¡ñÌsªdÈéê
                        else{
                            // ñÌ¤¿dÈéêÆdÈçÈ¢êðª
                            int sbdx=-blW+0+1+j;
                            int fbdx=j;
                            if(sbdx>=0&&sbdx<bdW&&fbdx>=0&&fbdx<bdW){
                                // bitZ
                                ll a=bk[k][l]<<(sbdx);
                                // êv·éÈçAcnt
                                if((a^bd[bdy])==bits[bdW])cnt++;
                                else if((a&bd[bdy])!=0)isSetable=false;
                            }
                            // ¼[Oêé
                            else if((sbdx<0||sbdx>=bdW)&&(fbdx<0||fbdx>=bdW)){
                                ll a=((bk[k][l]>>abs(sbdx))<<(64-bdW))>>(64-bdW);
                                ll b1=(bk[k][l]<<(64-abs(sbdx)))>>(64-abs(sbdx));
                                ll b2=(bk[k][l]>>abs(sbdx)+bdW);
                                if(b1==0&&b2==0){
                                    if((a^bd[bdy])==bits[bdW])cnt++;
                                    else if(((a&bd[bdy])!=0))isSetable=false;
                                }
                                else isSetable=false;
                            }
                            // E[ªÍÝoé
                            else if(sbdx>=0&&sbdx<bdW){
                                // ÍÝoéêð0Åmask
                                int mv=(64-(bdW-sbdx));
                                ll a=((bk[k][l]<<mv)>>mv)<<(sbdx);
                                // ÍÝo½ê
                                ll b=bk[k][l]>>(bdW-sbdx);
                                if(b==0){
                                    if((a^bd[bdy])==(bits[bdW]))cnt++;
                                    else if((a&bd[bdy])!=0)isSetable=false;
                                }
                                else isSetable=false;
                            }
                            // ¶[ªÍÝoé
                            else{
                                ll a=(bk[k][l]>>(blW-fbdx-1));
                                int mv=(64-(blW-fbdx-1));
                                ll b=(bk[k][l]<<mv)>>mv;
                                if(b==0){
                                    if((a^bd[bdy])==(bits[bdW]))cnt++;
                                    else if((a&bd[bdy])!=0)isSetable=false;
                                }
                                else isSetable=false;
                            }
                        }
                    }
                    if(isSetable)res=max(res,cnt);
                }
            }
        }
        cout<<res<<endl;
        prv=res;
    }
    return 0;
}

Measured memory_bytes: 1064