cho.sh
Notes
Loading...

Border-Crossing Salesman

Time limit

2s

Memory limit

128 MB

Problem

Long before international trade treaties, a salesman had to pay taxes at every border he crossed. Given two countries, find the minimum number of borders that must be crossed to travel from one to the other.

The surface of Earth is modeled as a closed convex three-dimensional polyhedron. Each polygonal face represents one country. You may not cross a border at a point where three or more countries meet.

Input

The first line contains the number of countries c (4 <= c <= 6000). The next c lines each have the form n x1 y1 z1 ... xn yn zn, describing the n vertices of one closed polygon in order (3 <= n <= 20).

The next line contains the number of queries m (0 < m <= 50). Each of the following m lines contains two country numbers ca cb. Countries are numbered starting from 1.

No vertex lies on the segment between two connected vertices. Every coordinate x, y, and z satisfies -10^6 <= x, y, z <= 10^6. Within one country, no two non-adjacent edges share a point.

Output

For each query, output the minimum number of borders that must be crossed to travel from ca to cb.