{-# LANGUAGE FlexibleContexts, BangPatterns #-}

module Main where

import Data.Char
import Data.List
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Control.Applicative
import Text.Printf


readInt :: IO Int
readInt = read <$> (input `seq` input)
    where input = do
            !c <- getChar
            if isDigit c || c == '-'
                then (c :) <$> input
                else return []

prints :: Show a => [a] -> IO ()
prints as = putStrLn $ concat $ intersperse " " $ map show as

main = do
    t <- getLine
    readInt
    inp <- map words . lines <$> getContents
    let f ((q:param):qs) t = case q of
            "replace" ->
                let [a', b', s] = param
                    a = read a'
                    b = read b'
                in f qs $ take a t ++ s ++ drop (b+1) t
            "reverse" ->
                let [a, b] = read <$> param
                in f qs $ take a t ++ reverse (drop a $ take (b+1) t) ++ drop (b+1) t
            "print" ->
                let [a, b] = read <$> param
                in putStrLn (drop a $ take (b+1) t) >> f qs t
        f _ _ = return ()
    f inp t
