import Control.Applicative

import Control.Monad
    (replicateM)
    
import Data.List
    (intercalate)

data Operator
    = Replace Int Int String
    | Reverse Int Int
    | Print Int Int

type Environment
    = (String, [String])

type Problem
    = (String, [Operator])

type Result
    = [String]

operate :: Environment -> Operator -> Environment

operate (text, outputs) (Replace from to sub)
    = (before ++ sub ++ after, outputs) where
        before = take from text
        after = drop (to + 1) text
        
operate (text, outputs) (Reverse from to)
    = (before ++ sub ++ after, outputs) where
        before = take from text
        sub = reverse $ drop from $ take (to + 1) text
        after = drop (to + 1) text
        
operate (text, outputs) (Print from to)
    = (text, outputs ++ [picked]) where
        picked = drop from $ take (to + 1) text

solve :: Problem -> Result

solve (text, operators)
    = snd $ foldl operate (text, []) operators

output :: Result -> String

output result
    = intercalate "\n" result
    
input :: IO Problem

input
    = do
        text <- getLine
        n <- read <$> getLine
        operators <- replicateM n inputOperator
        return (text, operators)
        
inputOperator :: IO Operator

inputOperator
    = parseOperator <$> words <$> getLine
            
parseOperator :: [String] -> Operator

parseOperator ["replace", from, to, sub]
    = Replace (read from) (read to) sub
    
parseOperator ["reverse", from, to]
    = Reverse (read from) (read to)
    
parseOperator ["print", from, to]
    = Print (read from) (read to)

main :: IO ()

main
    = putStr <$> output =<< solve <$> input