#include <stdio.h>

int main() {
    int stk[1000], top, n;
    while (top=-1, scanf("%d", &n) != EOF) {
        char c;
        for(int i=0; i<n&&scanf(" %c", &c); ++i){
            if(c=='A' && top==-1) printf("E\n");
            else if(c=='A') printf("%d\n", stk[top]);
            else if(c=='P') scanf("%d", &stk[++top]);
            else if(top>-1) --top;
        }
    }
}