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;
    }
    bool valid(int n) {
        return 0 <= n && n < N;
    }
    void solve() {
        auto dp = new bool[][](2, N);
        dp[0][0] = S[0] == T[0];
        dp[0][1] = S[0] == T[1].next;
        foreach (i; 0 .. M - 1) {
            dp[(i + 1) & 1][] = false;
            foreach (k; 0 .. N) {
                if (!(dp[i & 1][k])) continue;
                if (valid(k - 1))
                dp[(i + 1) & 1][k - 1] |= T[k - 1].prev == S[i + 1];
                if (valid(k + 1))
                dp[(i + 1) & 1][k + 1] |= T[k + 1]      == S[i + 1];
                if (valid(k + 2))
                dp[(i + 1) & 1][k + 2] |= T[k + 2].next == S[i + 1];
            }
        }
        auto last = dp[(M - 1) & 1].ptr;
        writeln(last[N - 2] || last[N - 1] ? "Yes" : "No");
    }
    int _T; scanf("%d\n", &_T);
    foreach (t; 0 .. _T) {
        input; solve;
    }
}