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);

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