Under construction

Still being built. Things may change

Antennas

Time limit8sMemory limit512 MB

Summary
Given a convex polygon with antennas inside, count the antenna pairs whose connecting line crosses neither of two removed walls, for each removal scenario.
Level

Hard8 of 10

Topics
Geometry, Sorting, Two pointers
Solved
No attempts yet

Problem

In a secret military base, a new communication technology is being tested. For the experiment, mm antennas were constructed inside.

The terrain around the base is perfectly flat, and the base, seen from above, is a convex polygon. The boundary of the polygon is a wall that protects the base from intruders and blocks radio waves from leaving the base, where foreign agents could intercept them.

Some construction work is required in the facility, and two of the polygon's walls must be torn down. This creates a security risk. If two spies stand outside the base so that two antennas lie on the line between them, and no wall blocks this line, the spies can listen to the communication between those two antennas.

For several possible scenarios of removing two walls, count the pairs of antennas that are compromised this way.

The picture shows the first case of the sample input. The base is a pentagon with four antennas, marked with small crosses. All lines between pairs of antennas are also shown.

Input

The first line contains the number of test cases zz (1 ≤ zz ≤ 200000). Each test case has the following format.

The first line of a test case contains an integer nn (3 ≤ nn ≤ 10), the number of vertices of the polygon. The next nn lines contain two integers each, the coordinates of the vertices in clockwise order. The vertices are numbered 0, 1, ..., nn-1 in the order they appear.

The next line contains an integer mm (2 ≤ mm ≤ 50000), the number of antennas inside the base. The following mm lines contain the coordinates of the antennas.

The next line contains an integer qq (1 ≤ qq ≤ 10), the number of scenarios. The last qq lines describe the scenarios. The ii-th line contains two integers aia_i and bib_i (0 ≤ aia_i < bib_i ≤ nn-1). It denotes removing walls aia_i and bib_i, and asks for the number of distinct lines that go through two antennas and cross neither the segment between vertices aia_i and aia_i+1 nor the segment between vertices bib_i and (bib_i+1) mod nn.

All coordinates are integers with absolute values at most 10910^9. In any single test case, all points of the input are distinct and no three of them are collinear.

Every test case, including the first, is preceded by a single empty line.

The sum of all mm values over all test cases does not exceed 300000.

Output

For every test case, print the answers to all of its scenarios in separate lines.

Examples1

  1. Example 1

    Input
    2
    
    5
    0 0
    0 5
    3 7
    6 5
    6 0
    4
    1 2
    1 3
    5 2
    5 3
    3
    0 3
    1 4
    1 2
    
    4
    -1 -1
    -1 1
    2 1
    2 -1
    2
    0 0
    1 0
    6
    0 1
    0 2
    0 3
    1 2
    1 3
    2 3
    
    Expected output
    4 1 0
    0 1 0 0 0 0