#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

string table[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};

int N,M;
int T[50010];
int S[50010];

bool ok;
bool dfs(int n,int m){
    if(m==M){
        if(N-n<=2){
            ok=true;
            return true;
        }
        return false;
    }
    if(ok)return true;

    //if((N-n+1)/2>M-m+1)return false;
    if(n>0&&(T[n-1]+11)%12==S[m]){
        if(dfs(n-1,m+1)){
            ok=true;
            return true;
        }
    }

    if(n+1<N&&T[n+1]==S[m]){
        if(dfs(n+1,m+1)){
            ok=true;
            return true;
        }
    }
    if(n+2<N&&(T[n+2]+1)%12==S[m]){
        if(dfs(n+2,m+1)){
            ok=true;
            return true;
        }
    }
    return false;
}



void solve(){
    cin>>N>>M;
    string hoge;
    rep(i,N){
        cin>>hoge;
        T[i]=find(table,table+12,hoge)-table;
    }
    rep(i,M){
        cin>>hoge;
        S[i]=find(table,table+12,hoge)-table;
    }

    ok=false;
    if(dfs(-1,0))cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

signed main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int T;cin>>T;
    while(T--)solve();
    return 0;
}