This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,i,j,k,l,r,x,y,z,w,s,t,h[1100000],ans,qu;
vector<pair<ll,ll>> v[1100000];
pair<ll,ll> pq;
multiset<ll> st;
pair<ll,pair<ll,ll>> p[1100000],q[1100000];
pair<ll,ll> f(ll x,ll y)
{
    ll i;
    pair<ll,ll> p={x,0},q;
    for(i=0;i<h[x];i++)
    {
        if(v[x][i].first==y)
            continue;
        q=f(v[x][i].first,x);
        if(q.second+v[x][i].second>p.second)
        {
            p={q.first,q.second+v[x][i].second};
        }
    }
    return p;
}
int main()
{
    scanf("%lld %lld %lld",&n,&qu,&w);
    for(i=1;i<n;i++)
    {
        scanf("%lld %lld %lld",&x,&y,&z);
        st.insert(-z);
        p[i]={x,{h[x],z}};
        q[i]={y,{h[y],z}};
        v[x].push_back({y,z});
        v[y].push_back({x,z});
        h[x]++;
        h[y]++;
    }
    for(i=1;i<=qu;i++)
    {
        scanf("%lld %lld",&x,&y);
        x=(x+ans)%(n-1);
        y=(y+ans)%w;
        x++;
        v[p[x].first][p[x].second.first].second=y;
        st.erase(st.find(-p[x].second.second));
        p[x].second.second=y;
        st.insert(-y);
         v[q[x].first][q[x].second.first].second=y;
        q[x].second.second=y;
        //pq=f(1,0);
        //ans=f(pq.first,0).second;
        if(n==2)
            ans=(*st.begin());
            else
            ans=(*st.begin())+(*(++st.begin()));
            ans*=-1;
        printf("%lld\n",ans);
    }
}
Compilation message (stderr)
diameter.cpp: In function 'int main()':
diameter.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%lld %lld %lld",&n,&qu,&w);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         scanf("%lld %lld %lld",&x,&y,&z);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         scanf("%lld %lld",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |