#include <iostream>
#include <queue>
#define int long long
using namespace std;
priority_queue<int>q[1010];
int n, p, t, x;
signed main() {
	cin >> n >> p;
	for (int i = 0; i < p; i++) {
		cin >> n >> t;
		if (!n) { cin >> x; q[t].push(x); }
		else if (q[t].size()) {
			if (n == 1)cout << q[t].top() << endl;
			else q[t].pop();
		}
	}
}
