Language: C++

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

#define ll long long
#define pb push_back
#define mp make_pair

int rating[200011] = {0};
int par[200011] = { -1};

struct cmp {
	bool operator()(const int& a, const int& b) {

		return rating[a] > rating[b];

	}
};

struct comp {
	bool operator()(const int& a, const int& b) {

		return rating[a] < rating[b];

	}
};

void solve() {

	int n, q;
	cin >> n >> q;
	map<int, set<int, cmp>> group;

	for (int i = 1; i <= n; i++) {
		cin >> rating[i];
		int g;
		cin >> g;
		group[g].insert(i);
		par[i] = g;

	}

	while (q--) {
		int s, g;
		cin >> s >> g;
		int gg = par[s];

		group[gg].erase(s);
		group[g].insert(s);

		set<int, comp> ss;
		for (auto x : group) {
			if (!x.second.empty()) {
				auto it = x.second.begin();
				int frst = *it;
				ss.insert(frst);
			}
		}



		auto itt = ss.begin();
		cout << rating[*itt] << endl;


	}

	/*for(auto x : group){
		cout << x.first << "->";
		for(auto y: x.second){
			cout << y << " ";
		}
		cout << endl;
	}*/



}


int main()
{

	ios::sync_with_stdio(0);
	cin.tie(0);

	ll T;
	T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}




}


Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; _Const_Link_type = const std::_Rb_tree_node<int>*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; key_type = int]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = cmp; _Alloc = std::allocator<int>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, cmp, std::allocator<int> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<int>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<int>, int>::rebind<int>; typename _Alloc::value_type = int; value_type = int]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:37:18:   required from here
   37 |                 group[g].insert(i);
      |                 ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const cmp&, const int&, const int&>' evaluates to false
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = comp; _Alloc = std::allocator<int>; _Const_Link_type = const std::_Rb_tree_node<int>*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = comp; _Alloc = std::allocator<int>; key_type = int]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = comp; _Alloc = std::allocator<int>]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = comp; _Alloc = std::allocator<int>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, comp, std::allocator<int> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<int>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<int>, int>::rebind<int>; typename _Alloc::value_type = int; value_type = int]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:55:14:   required from here
   55 |                                 ss.insert(frst);
      |                                 ~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const comp&, const int&, const int&>' evaluates to false