using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
Stack<int> stack = new Stack<int>();
for (int i = 0; i < N; i++)
{
int[] command = Console.ReadLine().Split().Select(int.Parse).ToArray();
if (command[0] == 1)
{
int number = command[1];
stack.Push(number);
}
else if (command[0] == 2)
{
if (stack.Count > 0)
{
stack.Pop();
}
}
else if (command[0] == 3)
{
if (stack.Count > 0)
{
Console.WriteLine(stack.Max());
}
}
else if (command[0] == 4)
{
if (stack.Count > 0)
{
Console.WriteLine(stack.Min());
}
}
}
Console.WriteLine(string.Join(", ", stack));
}
}
}