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 f(stairs, music, i, k)
  if i == music.size
    return k + 1 == stairs.size || k + 2 == stairs.size
  end

  tone = music[i]
  if k >= 1
    return true if tone == (stairs[k - 1] - 1) % 12 && f(stairs, music, i + 1, k - 1)
  end
  if k + 1 < stairs.size
    return true if tone == stairs[k + 1] && f(stairs, music, i + 1, k + 1)
  end
  if k + 2 < stairs.size
    return true if tone == (stairs[k + 2] + 1) % 12 && f(stairs, music, i + 1, k + 2)
  end
  false
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 f(stairs, music, 0, -1) ? 'Yes' : 'No'
end