#include<stdio.h>
#include<iostream>
#include<stack>
#include<queue>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
char c[10],d[20];
int n,m,i,k,num;
while(~scanf("%d%d",&n,&m))
{
k=0;
stack<string>s;
queue<string>q;
for(i=1; i<=m; i++)
{
scanf("%s",c);
if(strcmp(c,"Add")==0)
{
scanf("%s",d);
if(s.size()<n)
{
s.push(d);
}
else
{
q.push(d);
}
}
if(strcmp(c,"Del")==0)
{
if(!s.empty())
{
s.pop();
if(!q.empty())
{
s.push(q.front());
q.pop();
}
}
else
{
k=1;
}
}
if(strcmp(c,"Out")==0)
{
if(!q.empty())
{
q.pop();
}
else
{
k=1;
}
}
}
if(k==1)
{
printf("Error\n");
}
else
{
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}
}
return 0;
}
#include<iostream>
#include<stack>
#include<queue>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
char c[10],d[20];
int n,m,i,k,num;
while(~scanf("%d%d",&n,&m))
{
k=0;
stack<string>s;
queue<string>q;
for(i=1; i<=m; i++)
{
scanf("%s",c);
if(strcmp(c,"Add")==0)
{
scanf("%s",d);
if(s.size()<n)
{
s.push(d);
}
else
{
q.push(d);
}
}
if(strcmp(c,"Del")==0)
{
if(!s.empty())
{
s.pop();
if(!q.empty())
{
s.push(q.front());
q.pop();
}
}
else
{
k=1;
}
}
if(strcmp(c,"Out")==0)
{
if(!q.empty())
{
q.pop();
}
else
{
k=1;
}
}
}
if(k==1)
{
printf("Error\n");
}
else
{
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}
}
return 0;
}