Under construction

Still being built. Things may change

Traffickers

Time limit3.5sMemory limit1024 MB

Summary
Given a tree of cities and traffickers shuttling between city pairs, add or remove routes and count deliveries on a path within a time range.
Level

Medium7 of 10

Topics
Tree, Segment tree, Math
Solved
No attempts yet

Problem

Pablo is one of the most notorious men in the Wild West, with influence over NN cities connected by roads that form a tree. Pablo's trusted men are traffickers, and they transport merchandise nonstop.

Each trafficker is defined by a pair of cities (u,v)(u, v). The trafficker starts at time 0 in city uu and travels toward vv along the shortest path. At each integer time, the trafficker delivers one unit of merchandise and moves on to the next city. When a trafficker reaches vv and delivers there, at the next moment he teleports back to uu. So if he delivers in vv at time tt, he delivers in uu at time t+1t + 1. The traffickers follow their routes forever.

Pablo wants to know whether the system can be improved. He runs three types of queries.

  • 1 u v: Adds a trafficker traveling between uu and vv.
  • 2 u v: Removes a trafficker traveling between uu and vv. Such a trafficker is guaranteed to exist.
  • 3 u v t1 t2: Counts the total units of merchandise delivered by all current traffickers, in every city on the shortest path between uu and vv (both endpoints included), at times from t1t_1 to t2t_2 (both included).

Input

The first line contains the number of cities NN. The next N−1N - 1 lines each contain two integers uu and vv, meaning there is a direct road between those two cities.

The next line contains KK, the number of traffickers initially present. The next KK lines each contain two integers uu and vv, the pair of cities defining that trafficker's route.

The next line contains QQ, the number of queries. The next QQ lines each contain one query in the format described above.

Output

For each query of type 3, print the answer on a separate line.

Constraints

  • 1≤N≤30,0001 \le N \le 30{,}000
  • 0≤K≤50,0000 \le K \le 50{,}000
  • 1≤Q≤50,0001 \le Q \le 50{,}000
  • 0≤t1≤t2≤2,000,000,0000 \le t_1 \le t_2 \le 2{,}000{,}000{,}000
  • The path of each trafficker covers at most 20 cities, including its endpoints.

Examples1

  1. Example 1

    Input
    5
    1 2
    2 3
    2 4
    1 5
    1
    4 1
    3
    3 3 5 0 3
    1 2 5
    3 4 5 1 5
    
    Expected output
    2
    10