/**
  * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_9_D&lang=jp
  * String - Transformation
  */
object Main {

  object Int {
    def unapply(v: String): Option[Int] = {
      Some(v.toInt)
    }
  }

  def main(args: Array[String]): Unit = {
    val initialStr = io.StdIn.readLine()
    val q = io.StdIn.readInt()
    val printPattern = """print (\d+) (\d+)""".r
    val reversePattern = """reverse (\d+) (\d+)""".r
    val replacePattern = """replace (\d+) (\d+) (\w+)""".r
    Iterator.continually(io.StdIn.readLine()).takeWhile(_ != null).take(q)
      .foldLeft(initialStr) { (str, command) =>
        command match {
          case printPattern(Int(a), Int(b)) =>
            println(str.slice(a, b + 1))
            str
          case reversePattern(Int(a), Int(b)) =>
            str.take(a) + str.slice(a, b + 1).reverse + str.drop(b + 1)
          case replacePattern(Int(a), Int(b), p) =>
            str.patch(a, p, b - a + 1)
        }
      }
  }
}