#include <bits/stdc++.h>
using namespace std;
vector<bool>vis;
vector<int>dist,visina,listovi;
vector<vector<pair<int,int>>>adj;
int tr,pocetni;
int dfs(int v){
    vis[v]=true;
    int mn=0;
    for(auto u:adj[v]){
        if(!vis[u.first]){
            mn=INT_MAX;
            mn=min(mn,u.second+dfs(u.first));
        }
    }
    return mn;
}

void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans){
    adj.resize(n+3);
    dist.resize(n+3);
    visina.resize(n+3);
    vis.assign(n+3,false);
    for(int i=1;i<n;i++){
        adj[i+1].push_back({p[i+1],w[i+1]});
        adj[p[i+1]].push_back({i+1,w[i+1]});
    }

    
    for(int i=1;i<=q;i++){
        int rez=INT_MAX;
        for(int x=1;x<=n;x++){
            vector<int>duzine;
            for(auto ve:adj[x]){
                vis.assign(n+3,false);
                vis[x]=true;
                duzine.push_back(dfs(ve.first));
            }
            sort(duzine.begin(),duzine.end());
            if(duzine.size()>3){
                rez=min(rez,duzine[duzine.size()-1]+duzine[duzine.size()-2]+duzine[duzine.size()-3]);
            }
        }
    }

}