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

#LCA0000044. Khoảng cách giữa các đỉnh (Distance Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Khoảng cách giữa các đỉnh (Distance Queries)

Distance Queries

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree on nn vertices numbered from 11 to nn. Every edge has length 11, and there is exactly one simple path between any two vertices.

For every pair a,ba,b, report the number of edges on their unique path. If a=ba=b, the answer is 00.

Input

  • The first line contains n,qn,q: the numbers of vertices and queries.
  • Each of the next n−1n-1 lines contains u,vu,v, describing an undirected edge.
  • Each of the next qq lines contains two vertices a,ba,b to query.

Output

Print qq lines, each containing the number of edges on the corresponding path.

Subtasks

General constraints: 1≤n,q≤2⋅1051\le n,q\le 2\cdot10^5; 1≤a,b,u,v≤n1\le a,b,u,v\le n.

  • 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≤2000001\le n\le 200000; 1≤q≤2000001\le q\le 200000.

Examples

Input

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

Output

1
3
2

Explanation

Vertices 11 and 33 share one edge, giving distance 11. The path 2−1−3−52-1-3-5 has three edges. Finally, 1−3−41-3-4 has two edges; hence the three output lines are 1,3,21,3,2.