#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int T[50000];
int S[50000];

int main() {
  string hoge[] = {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  map<string, int> mp;
  REP(i,12) mp[hoge[i]] = i;
  int NUM;
  cin>>NUM;
  while(NUM--) {
    int n,m; cin >> n >> m;
    REP(i,n) {
      string s;cin >> s;
      T[i] = mp[s];
    }
    REP(i,m) {
      string s;cin>>s;
      S[i] = mp[s];
    }
    queue<pii> Q;
    Q.push(pii(-1,-1));
    bool ok = 0;
    while(!Q.empty()) {
      pii p = Q.front(); Q.pop();
      int a = p.first, b = p.second;

      int rest = n-a;
      if ((m-b)*2 < n-a) continue;
      if (a==n) {
        if (b==m) {
          ok = 1;
          break;
        }
        continue;
      }
      if (a+1<=n) {
        if (a+1==n || T[a+1]==S[b+1]) {
          Q.push(pii(a+1,b+1));
        }
      }
      if (a+2<=n) {
        if (a+2==n || (T[a+2]+1)%12==S[b+1]) {
          Q.push(pii(a+2,b+1));
        }
      }
      if (a) {
        if ((T[a-1]+11)%12==S[b+1]) {
          Q.push(pii(a-1,b+1));
        }
      }
    }
    cout << (ok?"Yes":"No") << endl;
  }

}