main = getContents >>= putStr.unlines.(\xs -> [fst x | x<-xs, fst x /= ""]).(\(x:_:xs) -> (scanl (flip ($)) ("",x) $map (f.words) xs) ).lines
    where f ("print":a:b:_)     (_,s) = (between a b s, s)
          f ("reverse":a:b:_)   (_,s) = ("", g a b s $reverse (between a b s))
          f ("replace":a:b:p:_) (_,s) = ("", g a b s p)
          r = read
          g a b s t = take (r a) s ++ t ++ drop ((r b)+1) s
          between a b = (take $(r b)-(r a)+1).drop (r a)