#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



int stair[50000],melody[50000];


bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

int getNUM(char buf[3]){
	switch(buf[0]){
	case 'C':
		if(buf[1] == '\0'){
			return 0;
		}else{
			return 1;
		}
		break;
	case 'D':
		if(buf[1] == '\0'){
			return 2;
		}else{
			return 3;
		}
		break;
	case 'E':
		return 4;
		break;
	case 'F':
		if(buf[1] == '\0'){
			return 5;
		}else{
			return 6;
		}
		break;
	case 'G':
		if(buf[1] == '\0'){
			return 7;
		}else{
			return 8;
		}
		break;
	case 'A':
		if(buf[1] == '\0'){
			return 9;
		}else{
			return 10;
		}
		break;
	case 'B':
		return 11;
		break;
	}
}

bool is_UP(int to,int from){

	if((to == from+1) || (to == 0 && from == 11))return true;
	else{
		return false;
	}
}

bool is_DOWN(int to,int from){
	if((to == from-1 || (to == 11 && from == 0)))return true;
	else{
		return false;
	}
}


void func(){

	int N,M;
	scanf("%d %d",&N,&M);

	char buf[3];

	for(int i = 0; i < N; i++){
		scanf("%s",buf);
		stair[i] = getNUM(buf);
	}
	reverse(stair,stair+N);

	for(int i = 0; i < M; i++){
		scanf("%s",buf);
		melody[i] = getNUM(buf);
	}
	reverse(melody,melody+M);

	bool FLG = false;
	int loc,index;

	if((stair[0] == melody[0]) || (N >= 2 && is_UP(melody[0],stair[0]) == true)){
		if(stair[0] == melody[0]){
			loc = 1;
		}else{
			loc = 2;
		}

		if(loc == N && M == 1){
			printf("Yes\n");
			return;
		}

		index = 1;

		while(loc < N || index < M){
			if(melody[index] == stair[loc]){
				loc += 1;
				index++;
			}else if(is_UP(melody[index],stair[loc]) == true && loc+2 <= N){
				loc += 2;
				index++;
			}else if(is_DOWN(melody[index],stair[loc]) == true && loc > 0){
				loc -= 1;
				index++;
			}else{
				break;
			}
		}
		if(loc == N && index == M)FLG = true;
	}

	if(FLG){
		printf("Yes\n");
		return;
	}

	if((N >= 2 && stair[1] == melody[0]) || (N >= 3 && is_UP(melody[0],stair[1]) == true) || (is_DOWN(melody[0],stair[1]) == true)){
		if(stair[1] == melody[0]){
			loc = 2;
		}else if(is_UP(melody[0],stair[1]) == true){
			loc = 3;
		}else{ //is_DOWN(melody[0],stair[1]) == true
			loc = 0;
		}

		if(loc == N && M == 1){
			printf("Yes\n");
			return;
		}

		index = 1;

		while(loc < N || index < M){
			if(melody[index] == stair[loc]){
				loc += 1;
				index++;
			}else if(is_UP(melody[index],stair[loc]) == true && loc+2 <= N){
				loc += 2;
				index++;
			}else if(is_DOWN(melody[index],stair[loc]) == true && loc > 0){
				loc -= 1;
				index++;
			}else{
				break;
			}
		}
		if(loc == N && index == M)FLG = true;
	}

	if(FLG){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
}


int main(){

	int N;
	scanf("%d",&N);

	for(int i = 0; i < N; i++)func();

	return 0;
}

Measured memory_bytes: 3416