import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val t = readLine.trim.toInt
  for (_ ← 0 until t){
    val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
    val boards = readLine.trim.split(' ').map(parseSound)
    val melodies = readLine.trim.split(' ').map(parseSound)
    println(
      if (canMake(boards, melodies)) "Yes"
      else "No"
    )
  }
  def parseSound(str: String): Int = {
    List("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B").indexOf(str)
  }

  def canMake(boards: Array[Int], melodies: Array[Int]): Boolean = {
    @tailrec
    def sub(position: Int, reversedMelodies: List[Int]): Boolean = {
      reversedMelodies match {
        case Nil if position == -1 ⇒ true
        case Nil ⇒ false
        case _ if !boards.indices.contains(position) ⇒ false
        case h::t if (h + 1) % 12 == boards(position) ⇒ sub(position + 1, t)
        case h::t if h == boards(position) ⇒ sub(position - 1, t)
        case h::t if (h + 11) % 12 == boards(position) ⇒ sub(position - 2, t)
        case _ ⇒ false
      }
    }
    val reversed = melodies.reverse.toList
    sub(boards.length - 1, reversed) || sub(boards.length - 2, reversed)
  }
  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
