#include <bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
	
	int q;
	cin >> q;
	multiset<int> s;
	while (q--) {
		int type, x;
		cin >> type >> x;
		if (type == 1) {
			s.insert(x);
		} else if (type == 2) {
			// s.count(x): so lan xuat hien gia tri x hoac s.find(x): iterator tro toi phan tu x trong tap hop
			if(s.count(x) == 0) {
				continue;
			}
			// if (s.find(x) == s.end()) {
			// 	continue;
			// }
			s.erase(s.find(x)); // s.erase(x) se xoa toan bo gia tri x trong tap hop
		} else if (type == 3) {
			// lower_bound(x): tra ve iterator tro toi phan tu nho nhat >= x, neu kco tro toi end
			// upper_bound(x): tra ve iterator tro toi phan tu nho nhat > x, neu kco tro toi end
			auto pos = s.upper_bound(x);
			if (pos == s.end()) {
				cout << - 1 << '\n';
			} else {
				cout << *pos << '\n';
			}
		} else {
			auto pos = s.upper_bound(x);
			if (pos == s.begin()) {
				cout << - 1 << '\n';
			} else {
				pos--;
				cout << *pos << '\n';
			}
		}
		// s.size(): kich thuoc
		// s.empty(): tra ve true neu s rong
		// s.clear(): xoa toan bo s
	}
	return 0;
}