import Data.List

main = do
  str <- getLine
  _ <- getLine
  getContents >>= process str . lines
  where
    process acc [] = return ()
    process acc (x:xs)
      | cmd == "print" = do
        print' params acc
        process acc xs
      | cmd == "reverse" = do
        process (reverse' params acc) xs
      | cmd == "replace" = do
        process (replace' params acc) xs
      where (cmd:params) = words x
    print' [a, b] xs = putStrLn $ slice a' b' xs
      where
        a' = read a :: Int
        b' = read b :: Int
    reverse' [a, b] str = (take a' str) ++ (reverse $ slice a' b' str) ++ (drop (b'+1) str)
      where
        a' = read a :: Int
        b' = read b :: Int
    replace' [a, b, p] str = (take a' str) ++ p ++ (drop (b'+1) str)
      where
        a' = read a :: Int
        b' = read b :: Int
    read' = lines
    slice from to xs = take (to - from + 1) (drop from xs)

