Yonsei University Point Game

Players paint tree nodes blue and queries ask for the sum of weighted distances from a node to all painted nodes.

Medium7Divide and conquerTreeDFSNo attempts yetTime limit5sMemory limit128 MB

Problem

Yeondori is in his second year, and he is in charge of the campus point game for the new students' orientation. In a point game, players walk to facilities all over campus and finish a mission at each spot, which is how they learn where everything is. The usual format sends a player from one spot to the next and never back. Yeondori played last year and found that he barely memorized a single location that way.

So he wrote new rules.

The game runs over NN spots on campus, numbered 00 through N1N-1. The NN spots are joined by N1N-1 roads that form a tree. Every road is bidirectional, there is no cycle, and every spot is reachable from every other spot. Roads can have different lengths. The distance between two spots is the total length of the roads on the unique path between them.

A player performs QQ missions in the order they are given. There are two kinds of mission.

  1. Visit spot AA and paint a blue locker there. If that spot is already painted, nothing changes.
  2. Compute the sum of the distances from spot AA to every spot whose locker is painted blue.

No spot is painted when the game starts. If a mission of the second kind comes while no locker is painted, its answer is 00.

Yeondori cannot work out the answers to the second kind of mission. Compute them for him.

Input

The first line contains the number of spots NN (1N1000001 \le N \le 100000).

Each of the next N1N-1 lines, the ii-th of them for 1iN11 \le i \le N-1, contains two integers AiA_i and BiB_i (0Ai<i0 \le A_i < i, 1Bi1001 \le B_i \le 100). Spot ii and spot AiA_i are joined by a road of length BiB_i.

The next line contains the number of missions QQ (1Q1000001 \le Q \le 100000).

Each of the next QQ lines contains two integers CiC_i and DiD_i (CiC_i is 11 or 22, 0Di<N0 \le D_i < N), written in the order the missions are performed. If CiC_i is 11, paint a blue locker at spot DiD_i. If CiC_i is 22, find the sum of the distances from spot DiD_i to every painted spot.

Output

For each mission with Ci=2C_i = 2, print the sum of the distances on its own line, in the order the missions are given.