#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 2e5+5;


int n;
vector<array<int,2>> g[MAXN];
int d[MAXN];
array<array<int,2>,3> b[MAXN];

bool updb(int x, array<int,2> w){
    if(w[0] >= b[x][0][0]){
        b[x][2] = b[x][1];
        b[x][1] = b[x][0];
        b[x][0] = w;
        return true;
    }
    else if(w[0] >= b[x][1][0]){
        b[x][2] = b[x][1];
        b[x][1] = w;
        return true;
    }
    else if(w[0] >= b[x][2][0]){
        b[x][2] = w;
        return true;
    }
    return false;
}

void dfs1(int x, int p){
    b[x][0] = b[x][1] = b[x][2] = {0,-1};
    for(auto [k,w] : g[x]){
        if(k == p) continue;
        d[k] = d[x]+w;
        dfs1(k,x);
        updb(x,b[k][0]);
    }
    updb(x,{d[x],x});
    //cout << x << ' ' << p << '\n';
    //cout << b[x][0][0] << ' ' << b[x][0][1] << "  " << b[x][1][0] << ' ' << b[x][1][1] << "  " << b[x][2][0] << ' ' << b[x][2][1] << '\n';
}

int res = 0;
void dfs2(int x, int p, int W){
    res = max(res, b[x][0][0] + b[x][1][0] + max(W, b[x][2][0] - d[x]) - 2*d[x]);
    for(auto [k,w] : g[x]){
        if(k == p) continue;
        int tmpw = 0;
        if(b[x][0][1] == b[k][0][1]) tmpw = b[x][1][0];
        else tmpw = b[x][0][0];
        tmpw -= d[x];
        tmpw = max(tmpw, W);
        tmpw += w;
        dfs2(k,x,tmpw);
    }
}

void Bumerang(int32_t n, int32_t q, int32_t* p, int32_t* w, int32_t* s, int32_t* k, long long* ans){
    if(1ll*n*q > 2e8) return;
    for(int i = 2; i <= n; ++i){
        g[i].push_back({p[i],w[i]});
        g[p[i]].push_back({i,w[i]});
    }
    for(int qq = 1; qq <= q; ++qq){
        int x = s[qq], w = k[qq];
        auto it = lower_bound(g[x].begin(), g[x].end(), array<int,2>{p[x],0});
        (*it)[1] = w;
        it = lower_bound(g[p[x]].begin(), g[p[x]].end(), array<int,2>{x,0});
        (*it)[1] = w;
        dfs1(1,1);
        res = 0;
        dfs2(1,1,0);
        ans[qq] = res;
    }
}
#undef int