DICT = {
  'C' => 0, 'C#' => 1, 'D' => 2, 'D#' => 3, 'E' => 4, 'F' => 5,
  'F#' => 6, 'G' => 7, 'G#' => 8, 'A' => 9, 'A#' => 10, 'B' => 11,
}

def check(stairs, music, k)
  (music.size - 1).downto(0) do |i|
    tone = music[i]
    stair = stairs[k]
    case tone
    when (stair + 1) % 12
      k -= 2
    when stair
      k -= 1
    when (stair - 1) % 12
      k += 1
    else
      return false
    end
    return false unless k >= -1 && k < stairs.size && k < i * 2
  end
  true
end

def work(stairs, music)
  (stairs.size >= 2 && check(stairs, music, stairs.size - 2)) ||
      check(stairs, music, stairs.size - 1)
end

s = STDIN.gets.to_i
s.times do
  STDIN.gets
  stairs = STDIN.gets.split.map{|x| DICT[x]}
  music = STDIN.gets.split.map{|x| DICT[x]}
  puts work(stairs, music) ? 'Yes' : 'No'
end