//47
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  int t;
  cin>>t;
  while(t--){
    int n,m;
    cin>>n>>m;
    string t[50000];
    for(int i=0;i<n;i++){
      cin>>t[i];
    }
    string s[50000];
    for(int i=0;i<m;i++){
      cin>>s[i];
    }
    int i;
    for(i=0;i<=1;i++){
      int x=n-1-i;
      int j;
      for(j=m-1;j>=0;j--){
	if(x<0||n<=x)break;
	static string sn[]={"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
	int c=find(sn,sn+12,t[x])-sn;
	if(sn[c]==s[j]){
	  x--;
	}else if(sn[(c+1)%12]==s[j]){
	  x-=2;
	}else if(sn[(c+11)%12]==s[j]){
	  x++;
	}else{
	  break;
	}
      }
      if(j<0&&x==-1){
	break;
      }
    }
    cout<<((i>1)?"No":"Yes")<<endl;
  }
  return 0;
}