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

#LCA0000035. Số đỉnh trên đường đi duy nhất (Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số đỉnh trên đường đi duy nhất (Tree)

Tree

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree contains nn vertices numbered 11 to nn. Each pair a,ba,b has one unique simple path. For every query, count the number of vertices on this path, including both endpoints.

Adding a new edge (a,b)(a,b) forms a cycle along the existing path; its number of vertices equals the number of vertices on the path. If a=ba=b, the path contains one vertex.

Input

  • First line: nn.
  • Next n−1n-1 lines: undirected edges u,vu,v.
  • Next line: number of queries qq.
  • Last qq lines: vertices a,ba,b.

Output

Print the number of vertices on each queried path.

Subtasks

General constraints (all subtasks): 2≤n≤1052\le n\le 10^5; 1≤q≤1051\le q\le 10^5; 1≤a,b≤n1\le a,b\le n.

  • Subtask 1 (20 points): 2≤n≤302\le n\le 30; 1≤q≤501\le q\le 50.
  • Subtask 2 (30 points): 2≤n≤10002\le n\le 1000; 1≤q≤10001\le q\le 1000.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

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

Output

3
3
1

Explanation

Path 3→2→43\to2\to4 contains 3 vertices, as does 1→2→31\to2\to3; the path from 22 to itself has 1 vertex.