#include<bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF 1001001001

int main(int, char**)
{
  int n, q;
  cin >> n >> q;
  vector< priority_queue<int> > a(n);

  vector<int> ans;
  rep(i,q) {
    int command;
    cin >> command;
    if (command == 0) {
      int t, x; cin >> t >> x;
      a[t].push(x);
    } else if (command == 1) {
      int t; cin >> t;
      if (!a[t].empty()) ans.push_back(a[t].top());
    } else {
      int t; cin >> t;
      if (!a[t].empty()) a[t].pop();
    }
  }
  for (auto a : ans) cout << a << endl;

  return 0;
}

