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

#LCA0000090. Cạnh ngắn nhất và dài nhất trên đường đi (Distance Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cạnh ngắn nhất và dài nhất trên đường đi (Distance Query)

Distance Query

Source: SPOJ

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 length. For each query between distinct cities a,ba,b, find the minimum and maximum individual edge lengths on their unique path, not the total distance.

Input

First line: nn; next n−1n-1 lines: u,v,wu,v,w. Then qq and qq queries a,ba,b.

Output

For every query print two integers: the minimum and maximum edge lengths.

Subtasks

  • Subtask 1 (20 points): 2≤n≤352\le n\le 35; 1≤q≤351\le q\le 35; 1≤u,v,a,b≤n1\le u,v,a,b\le n; a≠ba\ne b; 1≤w≤1061\le w\le10^6; 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,a,b≤n1\le u,v,a,b\le n; a≠ba\ne b; 1≤w≤1061\le w\le10^6; edges form a tree.

  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤q≤1000001\le q\le 100000; 1≤u,v,a,b≤n1\le u,v,a,b\le n; a≠ba\ne b; 1≤w≤1061\le w\le10^6; edges form a tree.

Examples

Input

5
2 3 100
4 3 200
1 5 150
1 3 50
3
2 4
3 5
1 2

Output

100 200
50 150
50 100

Explanation

Path 2−42-4 uses edges 100100 and 200200, so the result is 100 200100\ 200. Path 3−53-5 uses 5050 and 150150; path 1−21-2 uses 5050 and 100100.