PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000097. Cây nhiều màu (Colorful Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cây nhiều màu (Colorful Tree)

Colorful Tree

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree with nn vertices numbered from 11 to nn. Each edge has a color cc and a length dd. Colors are integers from 11 to n−1n-1.

For each of qq independent queries (x,y,u,v)(x,y,u,v), imagine replacing the length of every edge of color xx with yy. Output the resulting distance along the unique path between uu and vv. The original edge lengths are restored before the next query.

Input

  • The first line contains n,qn,q.
  • Each of the next n−1n-1 lines contains a,b,c,da,b,c,d: endpoints, color and length of an edge.
  • Each of the next qq lines contains x,y,u,vx,y,u,v.

Output

Print one answer per query.

Subtasks

  • Subtask 1 (20 points): 2≤n≤1052\le n\le 10^5, 1≤q≤1051\le q\le 10^5; 1≤a,b,u,v≤n1\le a,b,u,v\le n, a≠ba\ne b, u<vu<v; 1≤c,x≤n−11\le c,x\le n-1; 1≤d,y≤1041\le d,y\le 10^4. Các cạnh tạo thành một cây. Additionally, n≤10n\le 10 and the number of queries/plans is at most 1010.
  • Subtask 2 (30 points): 2≤n≤1052\le n\le 10^5, 1≤q≤1051\le q\le 10^5; 1≤a,b,u,v≤n1\le a,b,u,v\le n, a≠ba\ne b, u<vu<v; 1≤c,x≤n−11\le c,x\le n-1; 1≤d,y≤1041\le d,y\le 10^4. Các cạnh tạo thành một cây. Additionally, n≤2000n\le 2000 and the number of queries/plans is at most 20002000.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le 10^5, 1≤q≤1051\le q\le 10^5; 1≤a,b,u,v≤n1\le a,b,u,v\le n, a≠ba\ne b, u<vu<v; 1≤c,x≤n−11\le c,x\le n-1; 1≤d,y≤1041\le d,y\le 10^4. Các cạnh tạo thành một cây.

Examples

Input

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Output

130
200
60

Explanation

For the first query, the edge of color 11 contributes 100100 and the edge of color 44 contributes 3030, totaling 130130. The second path consists of two edges of color 11, giving 200200. For the third query, the queried color 33 does not occur on the path, so its length stays 20+10+30=6020+10+30=60.