module Main where

import Data.Functor

main :: IO ()
main =  do
  { str <- getLine
  ; [q] <- getInts
  ; loop q str
  }

loop :: Int -> String -> IO ()
loop 0 _ = done
loop m s = do
  { cmd <- getWords
  ; case cmd of
      ["print",a,b]     -> prn (read a) (read b) s >>  loop (m-1) s
      ["reverse",a,b]   -> rev (read a) (read b) s >>= loop (m-1)
      ["replace",a,b,p] -> rep (read a) (read b) p s >>= loop (m-1)
  }

prn :: Int -> Int -> String -> IO ()
prn a b s = case splitAt a s of
  (_,ys) -> putStrLn (take (b-a+1) ys)

rev :: Int -> Int -> String -> IO String
rev a b s = case splitAt a s of
  (xs,ys) -> case splitAt (b-a+1) ys of
    (zs,ws)  -> return (xs ++ reverse zs ++ ws)

rep :: Int -> Int -> String -> String -> IO String
rep a b t s = case splitAt a s of
  (xs,_) -> case splitAt b s of
    (_,ys) -> return (xs ++ t ++ ys)

done :: IO ()
done = return ()

getInts :: IO [Int]
getInts = map read . words <$> getLine

getWords :: IO [String]
getWords = words <$> getLine