#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

const int LIM = 60000;

map<string, int> tone;

void init(){
    tone["C"]  = 0;
    tone["C#"] = 1;
    tone["D"]  = 2;
    tone["D#"] = 3;
    tone["E"]  = 4;
    tone["F"]  = 5;
    tone["F#"] = 6;
    tone["G"]  = 7;
    tone["G#"] = 8;
    tone["A"]  = 9;
    tone["A#"] = 10;
    tone["B"]  = 11;
}

int st[LIM+1], me[LIM+1];
int S, N, M;

bool dfs(int pos, int num){
    if(pos == N) return num == M;
    if(num >= M || pos >= N) return 0;
    if(me[num] == st[pos]) return dfs(pos+1, num+1);
    if(me[num] == (st[pos] + 1) % 12) return dfs(pos+2, num+1);
    if(pos > 0 && me[num] == (st[pos] + 11) % 12) return dfs(pos-1, num+1);
    return false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    init();
    cin >> S;
    for(;S--;){
        cin >> N >> M;
        string str;
        for(int x = 0; x < N; x++){
            cin >> str;
            st[x] = tone[str];
        }
        for(int x = 0; x < M; x++){
            cin >> str;
            me[x] = tone[str];
        }
        reverse(st, st+N);
        reverse(me, me+M);
        bool res = false;
        res = dfs(0, 0) || dfs(1, 0);
        if(res){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}