import Control.Monad
import Control.Applicative
import Data.List
solve :: [String] -> IO ()
solve (str:q:cs) = do
    foldM_ ope str $ map words cs
    where
    slice :: String -> Int -> Int -> (String,String,String)
    slice str bgn end =
        let (a,b) = splitAt bgn str in
        let (c,d) = splitAt (1+end-bgn) b in
        (a,c,d)
    --
    ope :: String -> [String] -> IO String
    --
    ope str ["print",a,b] = do
        let [a',b'] = map read [a,b]
        let (_,c,_) = slice str a' b'
        putStrLn c
        return str 
    --
    ope str ["reverse",a,b] = do
        let [a',b'] = map read [a,b]
        let (l,c,r) = slice str a' b'
        return $ l ++ reverse c ++ r
    --
    ope str ["replace",a,b,p] = do
        let [a',b'] = map read [a,b]
        let (l,c,r) = slice str a' b'
        return $ l ++ zipWith (\a b->b) c p ++ r
    --
    ope _ _ = undefined

main =
    solve . lines =<< getContents                                         