Problem:
Charging System for Network

There is a network consisting of N machines (sequentially numbered from 0 to N-1) interconnected through N-1 bidirectional communication cables. Any two machines can perform bidirectional communication through one or more cables. From time to time, machines in the network are renewed. When a new machine is introduced, the cables directly connected to it are also replaced with thicker ones to cope with the increased volume of communication traffic.

The communication fee arising from communication traffic between any two machines is calculated by summing the charges assigned to all the cables routing via the two. A unique charging scheme is employed in this system: if the size of a cable is a multiple of K, then the cable is not charged (free of charge). Other cables are charged according to their sizes.

Based on the given information on the network topology and Q instructions, write a program to execute each instruction.

Increase the sizes of all cables directly connected to the machine x by d.

Report the communication charge between the machines s and t.

Input

The input is given in the following format.

N K
a_1 b_1 c_1
a_2 b_2 c_2
:
a_{N−1} b_{N−1} c_{N−1}
Q
query_1
query_2
:
query_Q

The first line provides the number of machines N (2 ≤ N ≤ 105) and the cable size K (1 ≤ K ≤ 105) (the reference value for determining free of charge cables). Each of subsequent N-1 lines provides the i-th cable information that directly connects two machines a_i and b_i (0 ≤ a_i < b_i ≤ N-1), followed by the cable’s initial size c_i (1 ≤ c_i ≤ 105). For any pair of machines, the number of cables directly connecting them is either one or zero. The next line following them provides the number of instructions Q (1 ≤ Q ≤ 105). Each of the Q lines following it provides the i-th instruction query_i, which is either one of the following two:

add x d

or

send s t

The instruction add x d increase the size of all cables directly connected to machine x (0 ≤ x ≤ N-1) by d (1 ≤ d ≤ 105).

The instruction send s t reports the charge imposed to the communication between the two machines s (0 ≤ s ≤ N-1) and t (0 ≤ t ≤ N-1), where s ≠ t.

At least one send instruction is included in the input information.

Output

For each send command, output the communication charge between s and t.

Sample Input 1

6 3
0 1 1
0 2 1
0 3 1
2 4 1
2 5 1
3
send 1 4
add 2 2
send 1 4

Sample Output 1

3
1

Solution in C++ (judge verdict: Runtime Error)

#include<bits/stdc++.h>
#define U [7<<15]={}
#define G )+((c=A[a]+A[P[a]]+C[a])%k?c:0)-M(
typedef long long L;using namespace std;map<L,L>T U;
L P U,E U,Z U,H U,C U,A U,O U,X U,B U,k,n,a,b,c;char s U;
L M(L f){return f?B[f]+M(f^f&-f):0;}
L S(L a,L i){for(a=M(i++G i);i<2e5;i+=i&-i)B[i]+=a;}
L D(L f){L d;for(map<L,L>::iterator I=T[f].begin();I!=T[f].end();I++)
  d=I->first,!Z[d]?P[d]=f,E[d]=E[f]+1,C[d]=I->second,Z[d]=1,D(d),Z[f]+=Z[d],Z[H[f]]<Z[d]?H[f]=d:0:0;}
L K(L f){a=X[f];n=X[b];return a-n?M(O[E[a]<E[n]?a=n,n=b,b=f,n:f]G O[a])+K(P[a]):abs(M(O[f])-M(O[b]));}
main(){for(cin>>n>>k;--n;*Z-=T[a][b]=T[b][a]=c)cin>>a>>b>>c;
       for(D(a=0);Z[n];a=H[P[++n]]-n?n:0)for(;!O[a];S(a=H[a],O[a]=++b))X[a]=n;
       for(cin>>a;cin>>s>>a>>b;S(H[a],O[a]),S(a,O[a]-1))*s&2?cout<<K(a)<<endl,0:A[a]+=b;}