Distance on Triangulation

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

Hard8Divide and conquerShortest pathGraphBFSNo attempts yetTime limit2sMemory limit256 MB

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 n3n-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 (4n500004 \le n \le 50000).

Each of the next n3n-3 lines contains two integers aia_i and bib_i, the endpoints of the ii-th diagonal (1ai,bin1 \le a_i, b_i \le n, aibia_i \ne b_i).

The next line contains an integer qq, the number of queries (1q1000001 \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 (1xi,yin1 \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.