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

set<pair<int, int>> l[200010];
void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans) {
	for(int i = 2; i <= n; i++) {
		l[i].emplace(w[i], p[i]);
		l[p[i]].emplace(w[i], i);
	}
	int c = 1;
	for(int i = 1; i <= n; i++) {
		if(l[i].size() > 1) {
			c = i;
			break;
		}
	}
	for(int i = 1; i <= q; i++) {
		l[s[i]].erase(l[s[i]].find({w[s[i]], p[s[i]]}));
		l[s[i]].emplace(k[i], p[s[i]]);
		l[p[s[i]]].erase(l[p[s[i]]].find({w[s[i]], s[i]}));
		l[p[s[i]]].emplace(k[i], s[i]);
		ll res = 0;
		auto it = l[c].rbegin();
		for(int i = 0; i < 3; i++) {
			res += (*it).first;
			it++;
		}
		ans[i] = res;
	}
}