Distance on Triangulation
You have a convex polygon. The vertices of the polygon are successively numbered from 1 to n. You also have a triangulation of this polygon, given as a list of n − 3 diagonals.
You are also given q queries. Each query consists of two vertex indices. For each query, find the shortest distance between these two vertices, provided that you can move by the sides and by the given diagonals of the polygon, and the distance is measured as the total number of sides and diagonals you have traversed.
Input
The first line contains an integer n (4 ≤ n ≤ 50 000) - the number of vertices of the polygon.
Each of the following n − 3 lines contains two integers a[i]
, b[i]
- the ends of the i-th diagonal (1 ≤ a[i]
, b[i]
≤ n, a[i]
≠ b[i]
).
The next line contains an integer q (1 ≤ q ≤ 100 000) - the number of queries.
Each of the following q lines contains two integers x[i]
, y[i]
- the vertices in the i-th query (1 ≤ x[i]
, y[i]
≤ n). It is guaranteed that no diagonal coincides with a side of the polygon, and that no two diagonals coincide or intersect.
Output
For each query output a line containing the shortest distance.