#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define ERROR 0
#define OK 1

typedef int Status;
typedef int ElemType;
typedef struct Node
{
	ElemType data;
	struct Node *next;
}Node;
typedef struct Stack
{
	Node *top;
}Stack;

Status Init(Stack *S)
{
	if(!S)
		return ERROR;
	S->top=NULL;
	return OK;
}
Status Empty(Stack S)
{
	return !S.top;
}
Status Push(Stack *S,ElemType e)
{
	Node *newnode=(Node *)malloc(sizeof(Node));
	if(!newnode)
		return ERROR;
	newnode->data=e;
	newnode->next=S->top;
	S->top=newnode;
	return OK;
}
Status Pop(Stack *S,ElemType *e)
{
	Node *p=S->top;
	*e=p->data;
	S->top=p->next;
	free(p);
	return OK;
}
Status Destroy(Stack S,ElemType e)
{
	if(!Empty(S))
		Pop(&S,&e);
	 return OK;
}


Status GetTop(Stack S,ElemType *e)
{
	if(Empty(S))
		return ERROR;
	*e=S.top->data;
	return OK;
}
int main()
{
	int n,i,e;
	char a;
	while((scanf("%d",&n)!=EOF))
	{

		if(n==0)
			return 0;
		Stack S;
		Init(&S);
		for(i=0;i<n;i++)
		{
			scanf("\n%c",&a);
			switch(a)
			{
				case'P':
					{
						scanf("%d",&e);
						Push(&S,e);
						if(i==n-1)
							printf("\n");
						continue;
					}
				case'O':
					{
						if(!Empty(S))
							Pop(&S,&e);
						if(i==n-1)
							printf("\n");
						continue;
					}
				case'A':
					{
						if(GetTop(S,&e))
							printf("%d\n",e);
						else
							printf("E\n");
						if(i==n-1)
							printf("\n");
						continue;
					}	
			}					
		}
		Destroy(S,e);
	}
	return 0;
}
/**************************************************************
	Problem: 2192
	User: admin
	Language: C
	Result: Accepted
	Time:12 ms
	Memory:1144 kb
****************************************************************/