GC.disable

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.downto(1) do |i|
    return false unless 1 <= k && k <= stairs.size && k <= i * 2
    tone = music[i - 1]
    stair = stairs[k - 1]
    case (tone - stair) % 12
    when 1
      k -= 2
    when 0
      k -= 1
    when 11
      k += 1
    else
      return false
    end
  end
  k == 0
end

def work(stairs, music)
  check(stairs, music, stairs.size - 1) || check(stairs, music, stairs.size)
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