import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
object Main extends App {
  import scala.io.StdIn._

  solve
  def solve: Unit = {
    val Array(q, lim) = readLine.trim.split(' ').map(_.toInt)
    if (q == 0 && lim == 0) return
    val arrayBuffer = ArrayBuffer[Int]()
    val countBit = new CountBit(q)
    val map = new mutable.HashMap[Int, Int]()
    var count = 0
    for (_ ← 0 until q){
      val Array(query, x) = readLine.trim.split(' ').map(_.toInt)
      query match {
        case 0 ⇒
          countBit.increment(arrayBuffer.size, 1)
          map.put(x, arrayBuffer.size)
          count += 1
          arrayBuffer += x
          if (count > lim){
            count -= 1
            countBit.increment(lowerBound(countBit, 1), -1)
          }
        case 1 ⇒
          countBit.increment(lowerBound(countBit, x), -1)
          count -= 1
        case 2 ⇒
          println(arrayBuffer(lowerBound(countBit, x)))
        case 3 ⇒
          countBit.increment(map(x), -1)
          count -= 1
        case _ ⇒ ???
      }
    }
    println("end")
    solve
  }
  def lowerBound(bit: CountBit, target: Int): Int = {
    var left = 0
    var right = bit.size
    while (left < right){
      val mid = (left + right) / 2
      if (bit(mid) < target){
        left = mid + 1
      }else {
        right = mid
      }
    }
    right
  }
  class CountBit(val size: Int) {
    private val mArray = Array.tabulate(size){_ ⇒ 0}
    def apply(position: Int): Int = {
      var index = position
      var result = 0
      while (index >= 0){
        result += mArray(index)
        index -= ~index & (index + 1)
      }
      result
    }
    def increment(position: Int, value: Int): Unit = {
      var index = position
      while (index < size){
        mArray(index) += value
        index += ~index & (index + 1)
      }
    }
  }
}
