This page is still under construction.

Parts of this page are still being built. What you see may change.

Distance on Triangulation

Time limit2sMemory limit256 MB

Summary
Answer the shortest path length along polygon sides and diagonals for many queried vertex pairs in a triangulated convex polygon.
Level

Hard8 of 10

Topics
Divide and conquer, Shortest path, Graph, BFS
Solved
No attempts yet

Problem

You have a convex polygon. Its vertices are numbered from 1 to nn in order around the boundary. You also have a triangulation of this polygon, given as n−3n-3 diagonals that do not cross each other.

You are given qq queries. Each query consists of two vertex numbers. Moving only along the sides of the polygon and along the given diagonals, find the shortest distance between the two vertices of the query. The distance is the number of sides and diagonals you traverse.

Input

The first line contains an integer nn, the number of vertices of the polygon (4≤n≤500004 \le n \le 50000).

Each of the next n−3n-3 lines contains two integers aia_i and bib_i, the endpoints of the ii-th diagonal (1≤ai,bi≤n1 \le a_i, b_i \le n, ai≠bia_i \ne b_i).

The next line contains an integer qq, the number of queries (1≤q≤1000001 \le q \le 100000).

Each of the next qq lines contains two integers xix_i and yiy_i, the two vertices of the ii-th query (1≤xi,yi≤n1 \le x_i, y_i \le n).

No diagonal coincides with a side of the polygon, and no two diagonals coincide or cross.

Output

For each query print the shortest distance on its own line.

Hint

The picture shows the polygon and the triangulation of the first example.

Examples2

  1. Example 1

    Input
    6
    1 5
    2 4
    5 2
    5
    1 3
    2 5
    3 4
    6 3
    6 6
    
    Expected output
    2
    1
    1
    3
    0
    
  2. Example 2

    Input
    4
    1 3
    16
    1 1
    1 2
    1 3
    1 4
    2 1
    2 2
    2 3
    2 4
    3 1
    3 2
    3 3
    3 4
    4 1
    4 2
    4 3
    4 4
    
    Expected output
    0
    1
    1
    1
    1
    0
    1
    2
    1
    1
    0
    1
    1
    2
    1
    0