1 program j01;
2 const maxn=300086;
3 var head:array[0..maxn]of longint;
4 data:array[0..2*maxn]of int64;
5 q,next:array[0..2*maxn]of longint;
6 size,now,maxu:array[0..maxn]of longint;
7 st:array[0..22,0..2*maxn]of longint;
8 fa:array[0..maxn]of longint;
9 dep:array[0..maxn]of int64;
10 sum1,sum2:array[0..20*maxn]of int64;
11 root,rosize,mnsize:longint;
12 fir:array[0..maxn]of longint;
13 vis:array[0..maxn]of boolean;
14 bin:array[0..2*maxn]of longint;
15 u,v,w,n,m,tt,aa,i,time,tot,j:longint;
16 a,ps:array[0..maxn]of longint;
17 line:array[0..20*maxn]of longint;
18 bg,ed:array[0..maxn]of longint;
19 ax,bx,ll,rr,ans:int64;
20
21
22 function max(a,b:int64):int64;inline;begin if a>b then exit(a) else exit(b);end;
23 function min(a,b:int64):int64;inline;begin if a<b then exit(a) else exit(b);end;
24
25 procedure swap(var a,b:longint);
26 var c:longint;
27 begin
28 c:=a;a:=b;b:=c;
29 end;
30
31 procedure addd(u,v,w:longint);
32 begin
33 inc(tt);q[tt]:=v;data[tt]:=w;next[tt]:=head[u];head[u]:=tt;
34 end;
35
36 procedure sort(l,r:longint);
37 var i,j,x,y:longint;
38 begin
39 i:=l;j:=r;x:=a[ps[(i+j)div 2]];
40 repeat
41 while a[ps[i]]<x do inc(i);
42 while x<a[ps[j]] do dec(j);
43 if i<=j then
44 begin
45 swap(ps[i],ps[j]);
46 inc(i);dec(j);
47 end;
48 until i>j;
49 if i<r then sort(i,r);
50 if l<j then sort(l,j);
51 end;
52
53 procedure dfsst(i,pre:longint);
54 var j:longint;
55 begin
56 inc(time);fir[i]:=time;st[0,time]:=i;
57 j:=head[i];
58 while j>0 do
59 begin
60 if q[j]<>pre then
61 begin
62 dep[q[j]]:=dep[i]+data[j];dfsst(q[j],i);
63 inc(time);st[0,time]:=i;
64 end;
65 j:=next[j];
66 end;
67 end;
68
69 function min_st(a,b:longint):longint;
70 begin
71 if fir[a]<fir[b] then exit(a) else exit(b);
72 end;
73
74 procedure getst;
75 var i,j:longint;
76 begin
77 bin[1]:=0;
78 for i:=2 to time do
79 if i and(i-1)=0 then bin[i]:=bin[i-1]+1 else bin[i]:=bin[i-1];
80 for i:=1 to bin[time] do
81 for j:=1 to time+1-(1 shl i) do
82 st[i,j]:=min_st(st[i-1,j],st[i-1,j+(1 shl(i-1))]);
83 end;
84
85 function lca(u,v:longint):longint;
86 var k:longint;
87 begin
88 u:=fir[u];v:=fir[v];if u>v then swap(u,v);k:=bin[v-u+1];
89 exit(min_st(st[k,u],st[k,v+1-(1 shl k)]));
90 end;
91
92 function dist(u,v:longint):int64;
93 begin
94 exit(dep[u]+dep[v]-2*dep[lca(u,v)]);
95 end;
96
97 procedure dfssize(i,pre:longint);
98 var j:longint;
99 begin
100 size[i]:=1;maxu[i]:=0;j:=head[i];
101 while j>0 do
102 begin
103 if(vis[q[j]]=false)and(q[j]<>pre)then
104 begin
105 dfssize(q[j],i);inc(size[i],size[q[j]]);
106 maxu[i]:=max(maxu[i],size[q[j]]);
107 end;
108 j:=next[j];
109 end;
110 end;
111
112 procedure dfsroot(i,pre:longint);
113 var j:longint;
114 begin
115 maxu[i]:=max(maxu[i],rosize-size[i]);
116 if maxu[i]<mnsize then begin root:=i;mnsize:=maxu[i]; end;
117 j:=head[i];
118 while j>0 do
119 begin
120 if(vis[q[j]]=false)and(q[j]<>pre)then dfsroot(q[j],i);
121 j:=next[j];
122 end;
123 end;
124
125 procedure dfstree(i,pre:longint);
126 var j,ro:longint;
127 begin
128 dfssize(i,0);mnsize:=maxlongint;rosize:=size[i];
129 dfsroot(i,0);ro:=root;now[ro]:=tot;bg[ro]:=tot;tot:=tot+rosize;ed[ro]:=tot-1;
130 vis[ro]:=true;fa[ro]:=pre;
131 j:=head[ro];
132 while j>0 do
133 begin
134 if(vis[q[j]]=false)then dfstree(q[j],ro);
135 j:=next[j];
136 end;
137 end;
138
139 procedure ins(x:longint);
140 var u,f:longint;d:int64;
141 begin
142 u:=x;sum1[now[u]]:=0;
143 while fa[u]<>0 do
144 begin
145 f:=fa[u];d:=dist(x,f);
146 sum1[now[f]]:=d;sum2[now[u]]:=d;
147 u:=f;
148 end;
149 u:=x;
150 while u>0 do
151 begin
152 line[now[u]]:=x;inc(now[u]);u:=fa[u];
153 end;
154 end;
155
156 function finddown(l,r,x:longint):longint;
157 var mid:longint;
158 begin
159 if x<=a[line[l]] then exit(l-1);
160 while l<r do
161 begin
162 mid:=(l+r)div 2;
163 if a[line[mid+1]]>=x then r:=mid else l:=mid+1;
164 end;
165 exit(l);
166 end;
167
168 function findup(l,r,x:longint):longint;
169 var mid:longint;
170 begin
171 if x<a[line[l]] then exit(l-1);
172 if x>=a[line[r]] then exit(r);
173 while l<r do
174 begin
175 mid:=(l+r)div 2;
176 if a[line[mid+1]]>x then r:=mid else l:=mid+1;
177 end;
178 exit(l);
179 end;
180
181 procedure solve(x:longint);
182 var u,f,l,r,pl,pr:longint;d:int64;
183 begin
184 l:=finddown(bg[x],ed[x],ll);r:=findup(bg[x],ed[x],rr);
185 inc(ans,sum1[r]-sum1[l]);u:=x;pl:=l;pr:=r;
186 while fa[u]<>0 do
187 begin
188 f:=fa[u];d:=dist(x,f);
189 l:=finddown(bg[f],ed[f],ll);r:=findup(bg[f],ed[f],rr);
190 ans:=ans+sum1[r]-sum1[l]+d*(r-l);
191 ans:=ans-(sum2[pr]-sum2[pl])-d*(pr-pl);
192 u:=f;pl:=l;pr:=r;
193 end;
194 end;
195
196 begin
197 readln(n,m,aa);
198 for i:=1 to n do begin read(a[i]);ps[i]:=i; end;
199 sort(1,n);
200 for i:=1 to n-1 do
201 begin
202 readln(u,v,w);addd(u,v,w);addd(v,u,w);
203 end;
204 dfsst(1,0);getst;
205 tot:=1;dfstree(1,0);
206 for i:=1 to n do ins(ps[i]);
207 for i:=1 to tot do
208 begin
209 inc(sum1[i],sum1[i-1]);inc(sum2[i],sum2[i-1]);
210 end;
211 for i:=1 to m do
212 begin
213 readln(u,ax,bx);
214 ll:=min((ax+ans)mod aa,(bx+ans)mod aa);
215 rr:=max((ax+ans)mod aa,(bx+ans)mod aa);
216 ans:=0;solve(u);writeln(ans);
217 end;
218 end.