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

set<ll> l;
void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans) {
	for(int i = 1; i <= n; i++) {
		l.insert(w[i]);
	}
	for(int i = 1; i <= q; i++) {
		l.erase(l.find(w[s[i]]));
		l.insert(k[i]);
		w[s[i]] = k[i];
		ll res = 0;
		auto it = l.rbegin();
		res += *it;
		it++;
		res += *it;
		it++;
		res += *it;
		ans[i] = res;
	}
}