printCmd :: [String] -> String -> IO String
printCmd (a:b:_) s = putStrLn res >> return (res)
  where
    res = drop (read a) . take (read b+1) $ s

reverseCmd :: [String] -> String -> IO String
reverseCmd (a:b:_) s = return (pre ++ (reverse rev) ++ aft)
  where
    pre = take (read a) s
    rev = drop (read a) . take (read b+1) $ s
    aft = drop (read b+1) s

replaceCmd :: [String] -> String -> IO String
replaceCmd (a:b:r:_) s = return (pre ++ r ++ aft)
  where
    pre = take (read a) s
    aft = drop (read b) s

command :: Int -> String -> IO ()
command 0 _ = return ()
command n s = do
  c <- getLine
  case getCommand c of
    "print"   -> printCmd (getArgument c) s >>= command (n-1)
    "reverse" -> reverseCmd (getArgument c) s >>= command (n-1)
    "replace" -> replaceCmd (getArgument c) s >>= command (n-1)
  where getCommand = head . words
        getArgument = tail . words

main = do
  w <- getLine
  n <- getLine
  command (read n) w