import Control.Applicative
import Control.Monad
import qualified Data.Vector as V
import Data.List
import Debug.Trace

data Melody = A | AS | B | BS | C | CS | D | DS | E | ES | F | FS | G | GS deriving (Show,Read,Eq,Enum)

solve :: Int -> V.Vector Melody -> [Melody] -> Bool
solve now _ [] = now == -1
solve now ts (s:ss)
    | now == -1 = True
    | now >= V.length ts = False
    | otherwise = any (\(x,y) -> ts V.! now == x && solve (now+y) ts ss) [(pred' s,-2),(s,-1),(succ' s,1)]

pred' :: Melody -> Melody
pred' m
    | m == A = GS
    | otherwise = pred m

succ' :: Melody -> Melody
succ' m
    | m == GS = A
    | otherwise = succ m

readMelody :: String -> Melody
readMelody (x:xs)
    | null xs = read $ x : []
    | otherwise = read $ x : "S"

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
        [n,m] <- map read . words <$> getLine
        ts <- V.fromList . map readMelody . words <$> getLine
        ss <- reverse . map readMelody . words <$> getLine
        putStrLn $ if any (\lst -> solve (n-1) ts lst || solve (n-2) ts lst) $ tails ss
            then "Yes"
            else "No"