main :: IO ()
main = do
    str <- getLine
    coms <- getLine >> fmap lines getContents
    putStr . unlines $ collectStrs str coms

collectStrs :: String -> [String] -> [String]
collectStrs str coms = snd $ foldl f (str, []) coms
    where f (s, ss) com
              | c == "print"   = (s, ss ++ [slice a b s])
              | c == "reverse" = (rev a b s, ss)
              | c == "replace" = (rep a b s (last args), ss)
              where (c:args) = words com
                    [a,b] = map read . take 2 $ args

rev :: Int -> Int -> [a] -> [a]
rev a b xs = take a xs ++ reverse (slice a b xs) ++ drop (b+1) xs

rep :: Int -> Int -> [a] -> [a] -> [a]
rep a b xs ys = take a xs ++ ys ++ drop (b+1) xs

slice :: Int -> Int -> [a] -> [a]
slice f t = take (t-f+1) . drop f
