import Control.Applicative

noPrint :: String
noPrint = "DO_NOT_PRINT_THIS"

splitAt3 :: Int -> Int -> [a] -> [[a]]
splitAt3 a b x = [f, m, l]
    where
        (f, ml) = splitAt a x
        (m, l) = splitAt (b - a + 1) ml

doCommand :: [String] -> String -> (String, String)
doCommand (c : a1 : a2 : ar) s
    | c == "print"   = (m, s)
    | c == "reverse" = (noPrint, (f ++ (reverse m) ++ l))
    | c == "replace" = (noPrint, (f ++ p ++ l))
        where
            [a, b] = map read [a1, a2]
            [f, m, l] = splitAt3 a b s
            p = concat ar

outputs :: String -> [[String]] -> [String]
outputs _ [] = []
outputs s (l : ls) = o : outputs r ls
    where (o, r) = doCommand l s

main :: IO ()
main = do
    s <- getLine
    _ <- getLine
    putStr =<< (unlines . filter (/= noPrint). outputs s . map words . lines) <$> getContents
    