#include <stdio.h>
#include <string.h>
char stack[201]={};
int front=-1,back=-1;
int main()
{
int n,i,x;
char a[201]={};
scanf("%d",&n);
for(i=0; i<n; i++){
scanf("%s",a);
if(a[1]=='u'){
scanf("%d )",&x);
stack[++back]=x;
}
else if(a[0]=='f'){
if(front!=back){
printf("%d\n",stack[front+1]);
}
else printf("-1\n");
}
else if(a[0]=='b'){
if(back!=front){
printf("%d\n",stack[back]);
}
else printf("-1\n");
}
else if(a[0]=='p'){
if(front!=back) front++;
}
else if(a[0]=='s'){
printf("%d\n",back-front);
}
else{
if(front==back) printf("true\n");
else printf("false\n");
}
}
return 0;
}