import java.io.*;
import java.util.*;
public class Main {
static class FastScanner {
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
in = is;
}
private int read() {
if (ptr >= len) {
ptr = 0;
try {
len = in.read(buffer);
return -1;
}
if (len <= 0) return -1;
}
return buffer[ptr++];
}
int nextInt() {
int c;
while ((c = read()) <= ' ') {
if (c == -1) return -1;
}
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
int val = c - '0';
while ((c = read()) > ' ') {
val = val * 10 + (c - '0');
}
return val * sign;
}
}
public static void main
(String[] args
) { FastScanner fs
= new FastScanner
(System.
in); StringBuilder out = new StringBuilder();
int Q = fs.nextInt();
ArrayDeque<Integer> q1 = new ArrayDeque<>();
PriorityQueue<Integer> q2 = new PriorityQueue<>();
while (Q-- > 0) {
int type = fs.nextInt();
if (type == 1) {
int x = fs.nextInt();
q1.addLast(x);
}
else if (type == 2) {
if (!q2.isEmpty()) {
out.append(q2.poll()).append('\n');
} else {
out.append(q1.pollFirst()).append('\n');
}
}
else { // type == 3
while (!q1.isEmpty()) {
q2.add(q1.pollFirst());
}
}
}
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHN0YXRpYyBjbGFzcyBGYXN0U2Nhbm5lciB7CiAgICAgICAgcHJpdmF0ZSBmaW5hbCBJbnB1dFN0cmVhbSBpbjsKICAgICAgICBwcml2YXRlIGZpbmFsIGJ5dGVbXSBidWZmZXIgPSBuZXcgYnl0ZVsxIDw8IDE2XTsKICAgICAgICBwcml2YXRlIGludCBwdHIgPSAwLCBsZW4gPSAwOwoKICAgICAgICBGYXN0U2Nhbm5lcihJbnB1dFN0cmVhbSBpcykgewogICAgICAgICAgICBpbiA9IGlzOwogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSBpbnQgcmVhZCgpIHsKICAgICAgICAgICAgaWYgKHB0ciA+PSBsZW4pIHsKICAgICAgICAgICAgICAgIHB0ciA9IDA7CiAgICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgICAgIGxlbiA9IGluLnJlYWQoYnVmZmVyKTsKICAgICAgICAgICAgICAgIH0gY2F0Y2ggKElPRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAobGVuIDw9IDApIHJldHVybiAtMTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gYnVmZmVyW3B0cisrXTsKICAgICAgICB9CgogICAgICAgIGludCBuZXh0SW50KCkgewogICAgICAgICAgICBpbnQgYzsKICAgICAgICAgICAgd2hpbGUgKChjID0gcmVhZCgpKSA8PSAnICcpIHsKICAgICAgICAgICAgICAgIGlmIChjID09IC0xKSByZXR1cm4gLTE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaW50IHNpZ24gPSAxOwogICAgICAgICAgICBpZiAoYyA9PSAnLScpIHsKICAgICAgICAgICAgICAgIHNpZ24gPSAtMTsKICAgICAgICAgICAgICAgIGMgPSByZWFkKCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaW50IHZhbCA9IGMgLSAnMCc7CiAgICAgICAgICAgIHdoaWxlICgoYyA9IHJlYWQoKSkgPiAnICcpIHsKICAgICAgICAgICAgICAgIHZhbCA9IHZhbCAqIDEwICsgKGMgLSAnMCcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB2YWwgKiBzaWduOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgRmFzdFNjYW5uZXIgZnMgPSBuZXcgRmFzdFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBTdHJpbmdCdWlsZGVyIG91dCA9IG5ldyBTdHJpbmdCdWlsZGVyKCk7CgogICAgICAgIGludCBRID0gZnMubmV4dEludCgpOwoKICAgICAgICBBcnJheURlcXVlPEludGVnZXI+IHExID0gbmV3IEFycmF5RGVxdWU8PigpOwogICAgICAgIFByaW9yaXR5UXVldWU8SW50ZWdlcj4gcTIgPSBuZXcgUHJpb3JpdHlRdWV1ZTw+KCk7CgogICAgICAgIHdoaWxlIChRLS0gPiAwKSB7CiAgICAgICAgICAgIGludCB0eXBlID0gZnMubmV4dEludCgpOwoKICAgICAgICAgICAgaWYgKHR5cGUgPT0gMSkgewogICAgICAgICAgICAgICAgaW50IHggPSBmcy5uZXh0SW50KCk7CiAgICAgICAgICAgICAgICBxMS5hZGRMYXN0KHgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKHR5cGUgPT0gMikgewogICAgICAgICAgICAgICAgaWYgKCFxMi5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgICAgICAgICBvdXQuYXBwZW5kKHEyLnBvbGwoKSkuYXBwZW5kKCdcbicpOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBvdXQuYXBwZW5kKHExLnBvbGxGaXJzdCgpKS5hcHBlbmQoJ1xuJyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7IC8vIHR5cGUgPT0gMwogICAgICAgICAgICAgICAgd2hpbGUgKCFxMS5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgICAgICAgICBxMi5hZGQocTEucG9sbEZpcnN0KCkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBTeXN0ZW0ub3V0LnByaW50KG91dCk7CiAgICB9Cn0K