between :: String -> [Int] -> String
between s [a,b] = take (b - a + 1) $ drop a s

print' :: String -> [Int] -> String
print' s x = between s x

reverse' :: String -> [Int] -> String
reverse' s [a,b] = (take a s) ++ (reverse $ between s [a,b]) ++ drop (b + 1) s

replace' :: String -> [Int] -> String -> String
replace' s [a,b] c = (take a s) ++ c ++ drop (b + 1) s

solve :: String -> [[String]] -> [String] -> [String]
solve _ [] plist = plist
solve s ((op:args):xs) plist
  | op == "print"   = solve s xs (print' s (map read args) : plist)
  | op == "reverse" = solve (reverse' s (map read args)) xs plist
  | op == "replace" = solve (replace' s ((map read . init) args) (last args)) xs plist

main = getContents >>= mapM_ putStrLn . reverse . (\(s:_:xs) -> solve s (map words xs) []) . lines