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, i, k)
  loop do
    if i == 0
      return k == -1
    end

    tone = music[i - 1]
    stair = stairs[k]
    if k >= 1 && tone == (stair + 1) % 12
      i -= 1
      k -= 2
      next
    end
    if tone == stair
      i -= 1
      k -= 1
      next
    end
    if k < stairs.size - 1 && tone == (stair - 1) % 12
      i -= 1
      k += 1
      next
    end
    return false
  end
end

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