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

#LCA0000059. Những phòng học cách đều (A and B and Lecture Rooms)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Những phòng học cách đều (A and B and Lecture Rooms)

A and B and Lecture Rooms

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A university has nn rooms linked by n−1n-1 undirected corridors forming a tree. Two students start in rooms a,ba,b and wish to meet in a room xx whose distance from aa equals its distance from bb. Distances count corridors traversed.

For every pair a,ba,b, count the rooms xx satisfying this equality. If a=ba=b, every room is valid.

Input

  • The first line contains nn.
  • The next n−1n-1 lines describe edges u,vu,v.
  • The following line contains qq.
  • The last qq lines each contain rooms a,ba,b.

Output

Print one integer per query: the number of rooms equidistant from its two endpoints.

Subtasks

General constraints: 1≤n,q≤1051\le n,q\le10^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≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

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

Output

5
0
3
2

Explanation

For (1,1)(1,1), all five rooms work. The path 2−1−3−42-1-3-4 has odd length, giving no equidistant room. On path 4−3−54-3-5, rooms 1,2,31,2,3 are equidistant from 44 and 55, so the count is 33. For (1,4)(1,4), rooms 33 and 55 are equally distant from endpoints, giving 22.