This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define INF 1000000000000000000
#define eps (1e-9)
#define MOD 1000000007
using namespace std;
 
#define int long long int
#define lld long double
#define pii pair<int, int> 
#define random mt19937 rnd(chrono::steady_clock::now().tine_since_epoch().count())
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace seg {
	int n;
	vector<int> lv, rv, mx, mn, ans, a, tag;
	int get(int L, int R) {	return (L + R) | (L != R); }
	void push(int L, int R) {
		int mid = (L + R) / 2, nd = get(L, R);
		int l = get(L, mid), r = get(mid + 1, R);
		tag[l] += tag[nd], tag[r] += tag[nd];
		lv[nd] -= tag[nd], rv[nd] -= tag[nd];
		mx[nd] += tag[nd], mn[nd] += tag[nd];
		tag[nd] = 0; 
	}
	void pull(int L, int R) {
		int mid = (L + R) / 2, nd = get(L, R);
		int l = get(L, mid), r = get(mid + 1, R);
		lv[nd] = max({lv[l] - tag[l], lv[r] - tag[r], 
			mx[l] + tag[l] - 2 * mn[r] - 2 * tag[r]});
		rv[nd] = max({rv[l] - tag[l], rv[r] - tag[r],
			mx[r] + tag[r] - 2 * mn[l] - 2 * tag[l]});
		mx[nd] = max(mx[l] + tag[l], mx[r] + tag[r]);
		mn[nd] = min(mn[l] + tag[l], mn[r] + tag[r]);
		ans[nd] = max({ans[l], ans[r], lv[l] - tag[l] + mx[r] + tag[r], 
			rv[r] - tag[r] + mx[l] + tag[l]});
	}
 	void build(int L, int R) {
		int nd = get(L, R), mid = (L + R) / 2;
		if(L == R) {
			lv[nd] = rv[nd] =  -a[L];
			mx[nd] = mn[nd] = a[L], ans[nd] = 0;
			return ;
		}
		build(L, mid);
		build(mid + 1, R);
		pull(L, R);
	}
	void init_(int _n, vector<int>_a) {
		n = _n, a = _a;
		mx.assign(2 * n + 1, -INF);
		mn.assign(2 * n + 1, INF);
		ans.assign(2 * n + 1, -INF);
		tag.assign(2 * n + 1, 0);
		lv.assign(2 * n + 1, -INF);
		rv.assign(2 * n + 1, -INF);
		build(1, n);
	}
	void modify(int L, int R, int l, int r, int v) {
		int mid = (L + R) / 2, nd = get(L, R);
		if(l > R || r < L) return;
		if(L != R) push(L, R);
		if(l <= L && r >= R) tag[nd] += v;
		else {
			modify(L, mid, l, r, v);
			modify(mid + 1, R, l, r, v);
			pull(L, R);
		}
	} 
	int ask() { return ans[get(1, n)]; }
	void change(int l, int r, int v) { modify(1, n, l, r, v); }
};
namespace solver {
	int n, timestamp;
	vector<int> es, cost, x, y;
	vector<int> in, out, D, d, d1;
	vector<vector<int>> mp;
	void init_(int _n) {
		n = _n, timestamp = 0;
		es.clear(), cost.clear();
		x.clear(), y.clear();
		in.assign(n + 1, 0);
		out.assign(n + 1, 0);
		D.assign(2 * n, 0);
		d.assign(n + 1, 0);
		d1.assign(n + 1, 0);
		mp.assign(n + 1, vector<int>());
	}
	void add_edge(int a, int b, int c) {
		mp[a].push_back(es.size());
		mp[b].push_back(es.size());
		es.push_back(a ^ b);
		x.push_back(a);
		y.push_back(b);
		cost.push_back(c);
	}
	void dfs(int x, int par, int dd, int dd1) {
		in[x] = ++ timestamp, D[timestamp] = x;
		d[x] = dd, d1[x] = dd1;
		for(auto i : mp[x]) if(i != par) {
			int to = es[i] ^ x;
			dfs(to, i, dd + 1, dd1 + cost[i]);
			D[++timestamp] = x; 
		}
		out[x] = timestamp;
	}
	void build() {
		vector<int> a(2 * n, 0);
		dfs(1, -1, 0, 0);
		rep(i, 1, 2 * n - 1) a[i] = d1[D[i]];
		vprint(all(D));
		seg::init_(2 * n - 1, a);	
	}
	int query(int id, int v) {
		int chg = -cost[id] + v;
		int a = x[id], b = y[id];
		if(d[a] < d[b]) swap(a, b);
		cost[id] = v;
		seg::change(in[a], out[a], chg);
		return seg::ask();
	}
};
using namespace solver;
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q, w, last = 0;
	cin >> n >> q >> w, init_(n);
	rep(i, 1, n - 1) {
		int a, b, c; 
		cin >> a >> b >> c;
		add_edge(a, b, c);
	}	
	build();
	while(q --) {
		int x, y; cin >> x >> y;
		x = (x + last) % (n - 1);
		y = (y + last) % w;
		int ans = query(x, y);
		last = ans;
		cout << ans << "\n";
	}
	return 0;
} 
Compilation message (stderr)
diameter.cpp:4: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
    4 | #pragma loop-opt(on)
      | 
diameter.cpp:24:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   24 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
      |             ^~~~
diameter.cpp:24:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   24 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
      |                     ^~~~| # | 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... |