

import sys
input = sys.stdin.readline
N = int(input())
stack = []
result = []
for _ in range(N):
cmd = input().strip()
if cmd[0] == '1':
_, x = cmd.split()
stack.append(int(x))
elif cmd == '2':
if stack:
result.append(str(stack.pop()))
else:
result.append('-1')
elif cmd == '3':
result.append(str(len(stack)))
elif cmd == '4':
result.append('0' if stack else '1')
elif cmd == '5':
if stack:
result.append(str(stack[-1]))
else:
result.append('-1')
print('\n'.join(result))