#include <iostream>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int array[n];
    int type;
    int x;
    int size = 0;
    for(int i = 0;i<n;i++){
        cin >> type >> x;
        if(type==1){
            array[size++] = x;
        }else{
            if(type==2){
                if(size==0){
                    cout<<-1<<endl;
                    continue;
                }
                cout<<array[size-1]<<endl;
            }
            if(type==3){
                if(size>0){
                    array[--size] = 0;
                }
            }
        }
    }
    return 0;
}