import Control.Applicative

main = do s <- getLine
          _ <- getLine
          xs <- map words . lines <$> getContents
          putStr . unlines . filter (/= "") $ solve s xs

solve :: String -> [[String]] -> [String]
solve _ [] = []
solve s (x:xs) = s' : solve s'' xs
            where (s',s'') = run s x

run :: String -> [String] -> (String,String)
run s (x:a:b:p) | x == "print" = (s',s)
                | x == "reverse" = ("",s'')
                | x == "replace" = ("",s''')
        where s' = take ((read b)-(read a)+1) $ drop (read a) s
              s'' = bf ++ (reverse s') ++ af
              s''' = bf ++ (head p) ++ af
              bf = take (read a) s
              af = drop ((read b)+1) s