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

#LCA0000092. Đường ngắn nhất và dài nhất (Min Max Roads)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường ngắn nhất và dài nhất (Min Max Roads)

Min Max Roads

Source: LightOJ

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn cities connected by n−1n-1 undirected roads forming a tree. Each road has a positive integer cost.

For every query specifying two distinct cities u,vu,v, find the minimum and the maximum individual road costs along the unique path between them. Do not compute the total cost of the route.

Input

The first line contains nn. Next are n−1n-1 edge lines u,v,wu,v,w, followed by qq and qq queries u,vu,v.

Output

Print qq lines, each containing the minimum and maximum edge cost in that order.

Subtasks

  • Subtask 1 (20 points): 2≤n≤352\le n\le 35; 1≤q≤351\le q\le 35; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v for queries; 1≤w≤1051\le w\le10^5; edges form a tree.

  • Subtask 2 (30 points): 2≤n≤12002\le n\le 1200; 1≤q≤12001\le q\le 1200; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v for queries; 1≤w≤1051\le w\le10^5; edges form a tree.

  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤q≤250001\le q\le 25000; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v for queries; 1≤w≤1051\le w\le10^5; edges form a tree.

Examples

Input

5
1 2 5
1 3 8
2 4 2
2 5 9
3
3 4
4 5
1 5

Output

2 8
2 9
5 9

Explanation

Path 3−1−2−43-1-2-4 has edge costs 8,5,28,5,2, yielding 2 8. Path 4−2−54-2-5 has costs 2,92,9, yielding 2 9. Path 1−2−51-2-5 has costs 5,95,9, yielding 5 9.