import std.stdio;
import std.ascii;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

enum F = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];

int[string] RMap(string[] xs) {
    int[string] ret;
    foreach (int i, x; xs) ret[x] = i;
    return ret;
}

int next(int n) {
    return (n + 1) % 12;
}
int prev(int n) {
    return (n + 12 - 1) % 12;
}

enum R = RMap(F);

void main() {
    int N, M;
    int[] T, S;
    void input() {
        scanf("%d %d\n", &N, &M);
        T = readln.chomp.split(" ").map!((x) => R[x]).array;
        S = readln.chomp.split(" ").map!((x) => R[x]).array;
    }
    void solve() {
        //writeln(T, S);
        bool[Tuple!(int, int)] memo;
        bool dfs(int i, int k) {
            //writeln([k, i]);
            if (i == M) return abs(N - k) <= 2;
            if (tuple(i, k) in memo) return memo[tuple(i, k)];
            if (k - 1 >= 0 && T[k - 1].prev == S[i]) 
                if (dfs(i + 1, k - 1)) return true;
            if (k + 1 < N && T[k + 1] == S[i])
                if (dfs(i + 1, k + 1)) return true;
            if (k + 2 < N && T[k + 2].next == S[i])
                if (dfs(i + 1, k + 2)) return true;
            return memo[tuple(i, k)] = false;
        }
        writeln(dfs(0, -1) ? "Yes" : "No");
    }
    int _T; scanf("%d\n", &_T);
    foreach (t; 0 .. _T) {
        input; solve;
    }
}