#include <iostream>
#include <stack>
using namespace std;
int main() {
int N;
cin >> N;
stack<int> s;
for (int i = 0; i < N; i++) {
int command;
scanf("%d", &command);
if (command == 1) {
int X;
scanf("%d", &X);
s.push(X);
} else if (command == 2) {
if (!s.empty()) {
printf("%d\n", s.top());
s.pop();
} else {
printf("%d\n", -1);
}
} else if (command == 3) {
printf("%d\n", s.size());
} else if (command == 4) {
printf("%d\n", (s.empty() ? 1 : 0));
} else if (command == 5) {
if (!s.empty()) {
printf("%d\n", s.top());
} else {
printf("%d\n", -1);
}
}
}
return 0;
}