#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//对于100\%100%的数据,有N≤200000,X≤10^8N≤200000,X≤10 的8次方
int main(int argc, char *argv[])
{
int N,stack[2000000],stk[2000000];//stk栈用于存放当前最大的数字,stack用于存放放进储物的数字
scanf("%d",&N);//包含了N个操作
int i,j,k;//j是stack头指针,k是stk的头指针
i=j=k=0;
int temp,w,wcon=0;//用于记录输入进来的第一个数字是什么,w用于记录临时重量,wcon表示最重重量
for(k=0;k<N;k++)//记录N次操作
{
scanf("%d",&temp);//接下来进行三个数字的判断
if(temp==0)
{
scanf("%d",&w);
stack[i++]=w;
if(w>wcon)//如果临时输入的重量大于最重重量
{
stk[j++]=w;
wcon=w;
}
else stk[j++]=wcon;
}
if(temp==1)//出栈
{
if(i!=0)
{
if(stk[j-1]>stk[j-2])wcon=stk[j-2];
stack[--i]=0;
stk[--j]=0;
}
}
if(temp==2)//查询
{
if(i!=0) printf("%d\n",stk[j-1]);
else printf("0");
}
}
return 0;
}