#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>


int main() {


    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    

    int arr[100001];

int ind;

int max;

int type;

int num;

int cnt;

    

max = -1;

scanf("%d", &cnt);

while(cnt--) {

scanf("%d", &type);

if(type == 1){

scanf("%d", &arr[ind]);

if(max < arr[ind])

max = arr[ind];

ind++;

} else if(type == 2) {

ind--;

max = -1;

for(int i = 0 ; i < ind ; i++) {

if(max < arr[i]) {

max = arr[i];

}

}

} else if(type == 3) {

printf("%d\n", max);

}

}

    return 0;

}