#include <stdio.h>
#include <stdbool.h>
#define MaxSize 1000
typedef struct test {
int data[MaxSize];
int top;
} Stack;
void InitStack(Stack* S) {
S->top = -1;
}
bool Push(Stack* S, int x) {
if (S->top == MaxSize - 1)
return false;
else {
S->data[++S->top] = x;
return true;
}
}
bool Pop(Stack* S, int* x) {
if (S->top == -1)
return false;
else {
*x = S->data[S->top--];
return true;
}
}
bool GetTop(Stack S, int* x) {
if (S.top == -1)
return false;
else {
*x = S.data[S.top];
return true;
}
}
int main() {
int n;
while ((scanf("%d", &n)) != EOF)
{
Stack S;
InitStack(&S);
while (n--) {
getchar();
char ch;
scanf("%c",&ch);
if (ch == 'A') {
int x = 0;
if (!GetTop(S, &x))
printf("E\n");
else {
GetTop(S, &x);
printf("%d\n", x);
}
} else if (ch == 'P') {
int z=0;
scanf(" %d",&z);
Push(&S, z);
} else if (ch == 'O') {
int y = 0;
Pop(&S, &y);
}
}
}
return 0;
}