trait Command
case class Print(a: Int, b: Int)
case class Reverse(a: Int, b: Int)
case class Replace(a: Int, b: Int, p: String)

object Main extends App {
  val scanner = new java.util.Scanner(System.in)

  val str = scanner.next()
  val q = scanner.nextInt()

  val commands = (1 to q).map { _ =>
    scanner.next() match {
      case "print"   =>
        Print(scanner.nextInt(), scanner.nextInt())
      case "reverse" =>
        Reverse(scanner.nextInt(), scanner.nextInt())
      case "replace" =>
        Replace(scanner.nextInt(), scanner.nextInt(), scanner.next())
    }
  }

  commands.foldLeft(str) { (acc, command) =>
    command match {
      case Print(a, b)      =>
        println(acc.substring(a, b+1))
        acc
      case Reverse(a, b)    =>
        acc.take(a) ++ acc.substring(a, b+1).reverse ++ acc.drop(b+1)
      case Replace(a, b, p) =>
        acc.take(a) ++ p ++ acc.drop(b+1)
    }
  }
}