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

#LCA0000045. Dạo chơi đồng cỏ (Pasture Walk)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dạo chơi đồng cỏ (Pasture Walk)

Pasture Walk

Source: VNOJ

Version: Phuoc Hung OJ Extended

Problem Statement

A farm contains nn pastures, numbered from 11 to nn. Exactly n−1n-1 bidirectional roads connect all pastures, forming a tree. Every road has an associated length.

For each pair of pastures requested by the cows, calculate the total length of the unique route connecting them.

Input

  • The first line contains n,qn,q.
  • The next n−1n-1 lines each contain a,b,wa,b,w: road endpoints and length.
  • The last qq lines each contain two distinct pastures u,vu,v.

Output

Print the total route length for every query, one integer per line.

Subtasks

General constraints: 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le1000; 1≤w≤1041\le w\le10^4; 1≤u,v≤n1\le u,v\le n; u≠vu\ne v.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

Examples

Input

4 2
2 1 2
4 3 2
1 4 3
1 2
3 2

Output

2
7

Explanation

Pastures 11 and 22 are directly connected by a road of length 22. The path from 33 to 22 is 3−4−1−23-4-1-2, whose edge lengths are 2,3,22,3,2, totaling 77.