#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <sstream>

using namespace std;

int N; // ???????????°???????????§??????2^n???????????§????????°?????\????????¨??????
int BIT[1000000];

void add(int a, int w){
	
	// a?????????????´????w????¶???? O(log N)
	
	for(int x = a; x <= N; x += x & -x){
		BIT[x] += w;
	}
}

int sum(int a){
	
	// 1??????a?????§???????´????????¨?????????? O(log N)
	
	int ret = 0;
	for(int x = a; x > 0; x -= x & -x){
		ret += BIT[x];
	}
	return ret;
}

void init(){
	for(int i = 0; i < N; i++){
		BIT[i] = 0;
	}
}

// BIT ??????????¢?????????????????????????????´???????????????§????????¨???????????????
int lowerBound(int w){
	if(w <= 0){
		return 0;
	}
	int x = 0;
	for (int k = 262144; k > 0; k /= 2) { // k ??????????????? N ??\???????????§??? 2 ????????¨????????¨??????????°??????§?????????????????????
		if (x + k <= N && BIT[x + k] < w) {
			w -= BIT[x + k];
			x += k;
		}
	}
	return x + 1;
}

int main(){
	
	int q, lim;
	
	while(true){
		
		cin >> q >> lim;
		
		if(q == 0 && lim == 0){
			break;
		}
		
		N = 1000000 - 1;
		
		init();
		
		map<int, int> m1; // ID -> index
		map<int, int> m2; // index -> ID
		
		int index_ref = 1;
		int count = 0;
		
		for(int loop = 0; loop < q; loop++){
			int query, x;
			cin >> query >> x;
			if(query == 0){
				add(index_ref, 1);
				m1[x] = index_ref;
				m2[index_ref] = x;
				index_ref++;
				count++;
			}else if(query == 1){
				int index = lowerBound(x);
				add(index, -1);
				count--;
			}else if(query == 2){
				int index = lowerBound(x);
				cout << m2[index] << endl;
			}else{
				add(m1[x], -1);
				count--;
			}
			if(count > lim){
				int index = lowerBound(1);
				add(index, -1);
				count--;
			}
		}
		cout << "end" << endl;
	}
	
	return 0;
}