#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
 
int main()
{
    int q, lim, query, x, incubatorcnt;
    vector<int> List;
    vector<int>::iterator it;
    while(scanf("%d %d", &q, &lim) && q > 0)
    {
		incubatorcnt = 0;
        for(int f1 = 1; f1 <= q; f1++){
            scanf("%d %d",&query, &x);
            switch(query)
            {
                case 0:
                    List.push_back(x);
					incubatorcnt++;
					#if 0
                    if((int)List.size() > lim){
                        it = List.begin();
                        List.erase(it);
                    }
					#else
					if(incubatorcnt > lim){
                        it = List.begin();
                        List.erase(it);
						incubatorcnt--;
                    }
					#endif
                break;
                case 1:     
                    it = List.begin() + (x - 1);
                    List.erase(it);
					incubatorcnt--;
                break;
                case 2:
                    it = List.begin() + (x - 1);
                    printf("%d\n", *it);
                break;
				default:
					#if 0
					it = List.begin();
                    while(*it != x)
                        it++;
					#else
					it = find(List.begin(), List.end(), x);
					#endif
                    List.erase(it);
					incubatorcnt--;
                break;
            }
        }
        printf("end\n");
        List.clear();
    }
    return 0;
}