
scale = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
index = Hash[*([scale, (0...scale.size).to_a].transpose).flatten]

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  t = gets.split
  s = gets.split
  ok = false
  nowT = n-1
  nowS = m-1
  m.times do
    case t[nowT]
      when scale[index[s[nowS]]]
        nowT -= 1
      when scale[(index[s[nowS]] + 1) % scale.size]
        nowT += 1
      when scale[(index[s[nowS]] - 1 + scale.size) % scale.size]
        nowT -= 2
    end
    nowS -= 1
    ok = true if nowT == 0 && nowS == 0
    ok = true if nowT == -1 && nowS == -1
    break if nowT < 0 || n <= nowT
  end
  nowT = n-2
  nowS = m-1
  m.times do
    case t[nowT]
      when scale[index[s[nowS]]]
        nowT -= 1
      when scale[(index[s[nowS]] + 1) % scale.size]
        nowT += 1
      when scale[(index[s[nowS]] - 1 + scale.size) % scale.size]
        nowT -= 2
    end
    nowS -= 1
    ok = true if nowT == 0 && nowS == 0
    ok = true if nowT == -1 && nowS == -1
    break if nowT < 0 || n <= nowT
  end
  puts ok ? "Yes" : "No"
end