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

string c[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
int main() {
  int T,n,m,f,x;
  cin>>T;
  while(T--){
    cin>>n>>m;
    string s[n],t[m];
    for(int i=0;i<n;i++)cin>>s[i];
    for(int i=0;i<m;i++)cin>>t[i];
    f=0;
    for(int k=1;k<3;k++){
      x=n-k;
      for(int i=m-1,p;i>=0;i--){
        if(x<0||x>=n)goto e;
        p=0;
        while(c[p]!=s[x])p++;
        if(t[i]==c[p])x--;
        else if(t[i]==c[(p+1)%12])x-=2;
        else if(t[i]==c[(p+11)%12])x++;
        else goto e;
      }
      if(x==-1)f=1;
    e:;
    }
    cout<<(f?"Yes":"No")<<endl;
  }
  return 0;
}
