// AOJ 1083
#include<cstdio>
#include<cstring>
#include<map>
#define rep(i,a) for( int i = 0; i != (a); ++i )
#define clr(a,v) memset( (a), (v), sizeof(a) )

int q, lim;
int sz, dat[(1<<20)+2];
int low = (1<<19)-1;
std::map<int, int> num;
int rnum[400000];

void add( int x )
{
	num[x] = sz;
	dat[sz] = 0;
	rnum[sz++-low] = x;
	--lim;

	int k = sz-1;
	while( k )
	{
		++dat[k];
		k = (k-1)>>1;
	}

	return;
}

int find( int x )
{
	int k = 0;

	while( k < low )
	{
		int left = (k<<1)+1,
			right = (k<<1)+2;

		if( x <= dat[left] )
			k = left;
		else
			x -= dat[left], k = right;
	}

	return k;
}

void erase( int k )
{
	++lim;

	while( k )
	{
		--dat[k];
		k = (k-1)>>1;
	}

	return;
}

int main()
{
	while( scanf( "%d%d", &q, &lim ), q|lim )
	{
		clr( dat, 0 );
		clr( rnum, 0 );
		num.clear();
		sz = low;

		rep( i, q )
		{
			int query, x;
			scanf( "%d%d", &query, &x );

			if( !query )
			{
				add( x );

				if( lim < 0 )
					erase( find( 0 ) );
			}
			else if( query == 1 )
				erase( find( x ) );
			else if( query == 2 )
				printf( "%d\n", rnum[find(x)-low] );
			else
				erase( num[x] );
		}

		puts("end");
	}

	return 0;
}