#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <numeric>
#include <climits>
#include <iterator>
#include <iomanip>
#include <stack>
#include <set>
#include <cstdio>
#include <bitset>
#include <functional>
#include <tuple>
#include <list>
#include <deque>
using namespace std;

typedef pair<int, int> P;

#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef vector<int> VI;

int main() {
    cin.tie(0);
   	ios::sync_with_stdio(false);
    
    int n, q; cin >> n >> q;
    
    priority_queue<int>pq[n];
    
    REP(i, q){
        int a, b, c; cin >> a;
        if(a==0){
            cin >> b >> c;
            pq[b].push(c);
        }
        if(a==1){
            cin >> b;
            if(pq[b].size()==0) continue;
            
            cout << pq[b].top() << endl;
        }
        if(a==2){
            cin >> b;
            if(pq[b].size()==0) continue;
            
            pq[b].pop();
        }
    }
	return 0;
}
