#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i=0; i < (n); i++)

map<string, int> onkai;

int numOnkai = 0;

bool isok(int curT, int curS, int n, vector<int>& t, vector<int>& s) {
  while (curS >= 0 && curT >= 0 && curT < n) {
//    cout << "now:" << curT << ","<< t[curT] <<","<< s[curS] << endl;
    if (t[curT] == s[curS]) {
      curT--;
    }
    else if ((t[curT] + 1) % numOnkai == s[curS]) {
      curT -= 2;
    }
    else if (t[curT] == (s[curS] + 1) % numOnkai) {
      curT++;
    }
    else {
      break;
    }
    curS--;
  }
  return curS == -1 && ( curT == -1
  );
}

bool solve() {
  int n, m;
  cin >> n >> m;
  vector<int> t(n);
  vector<int> s(m);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    t[i] = onkai[s];
  }
  for (int i = 0; i < m; i++) {
    string st;
    cin >> st;
    s[i] = onkai[st];
  }
  return isok(n-1, m-1, n, t, s) || n > 1 && isok(n-2, m-1, n, t, s);

}

int main() {
  int n;
  cin >> n;

  int a=0;
  onkai["C"] = a++;
  onkai["C#"] = a++;
  onkai["D"] = a++;
  onkai["D#"] = a++;
  onkai["E"] = a++;
  onkai["F"] = a++;
  onkai["F#"] = a++;
  onkai["G"] = a++;
  onkai["G#"] = a++;
  onkai["A"] = a++;
  onkai["A#"] = a++;
  onkai["B"] = a++;
  numOnkai = a;

  for (int i = 0; i < n; i++) {
    cout << (solve() ? "Yes" : "No") << endl;
  }
  
  return 0;
}