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

long long int suma(multiset<long long int>& skup){
    int tr = 0;
    long long int vr = 0;
    for(long long int x : skup){
        if(tr == 3){
            break;
        }
        vr += x;
        tr++;
    }
    return (vr * (long long)-1);
}

void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans){
    multiset<long long int> skup;
    vector<long long int> a(n + 1);
    for(int i = 1; i <= n; i++){
        a[i] = w[i];
        skup.insert((long long)-1 * w[i]);
    }
    for(int test = 1; test <= q; test++){
        int starox = a[s[test]];
        int novox = k[test];
        a[s[test]] = k[test];
        skup.erase(skup.find((long long)-1 * starox));
        skup.insert((long long)-1 * novox);
        ans[test] = suma(skup);
    }
}