Under construction

Still being built. Things may change

Mineshaft

Time limit1sMemory limit128 MB

Summary
Run a pipe of straight segments up a winding polygonal shaft so each segment touches the walls in at least two places and the number of bends is minimal.
Level

Hard9 of 10

Topics
Geometry, Graph, Shortest path
Solved
No attempts yet

Problem

A plumber has been hired to run an air pipe through a mineshaft, from the bottom of the shaft up to the surface. The shaft was dug without modern equipment, so it winds its way up through the earth. Carrying the pipe bending tools down the shaft takes a great deal of time, so the plumber wants to minimize the number of bends in the pipeline.

For the mineshaft in the first picture below, the smallest possible number of bends is two. Several optimal pipelines exist, and the second picture shows one of them. The bullets mark the bends.

Each wall of the mineshaft is a sequence of straight segments, and the two walls need not have the same number of segments. The horizontal distance between the walls varies as well, but it is always positive. Both walls start at the same level and end at the same level.

On the way from the bottom of the shaft to the surface, the level (the yy coordinate) increases along every segment of a wall. The shaft therefore has no horizontal plateaus or ceilings, and at no point does it go back down.

For this task the diameter of the pipeline is 00. The pipeline may never cross a wall. To attach the pipeline firmly, every segment of the pipeline has to touch the walls in at least two different places. The bending points are weak and cannot be used to attach the pipeline. The end points of the pipeline, at the bottom and at the top of the shaft, may attach a segment, but only where they touch a wall.

That is why the third picture above, which also has two bends, is not allowed. Its lowest segment touches the walls in only one place, the foot of the right wall.

The pipeline must start anywhere at the bottom of the mineshaft and must end anywhere on the imaginary line between the top of the left wall and the top of the right wall. An end point of the pipeline attaches the pipeline only when it touches a wall. In particular, an end point at the bottom attaches nothing unless it sits exactly at the foot of a wall.

The angle α\alpha that the pipeline makes at a bending point may take any value with −180∘<α<180∘-180^\circ < \alpha < 180^\circ and α≠0\alpha \neq 0.

Letting the pipeline intersect itself is sometimes useful. In the mineshaft below it is the only way to get from the bottom to the top with three bends.

Input

The first line contains the number of test cases. Each test case has the following format.

  • One line with one integer N1N_1 (2≤N1≤25)(2 \le N_1 \le 25), the number of points describing the left wall of the mineshaft.
  • N1N_1 lines with the points describing the left wall, from the bottom to the top. The iith line contains two integers xix_i and yiy_i (−1000≤xi≤1000(-1000 \le x_i \le 1000, 0≤yi≤1000)0 \le y_i \le 1000) separated by a single space, the xx and yy coordinate of the point. The yy coordinates increase: y1<y2<⋯<yN1y_1 < y_2 < \cdots < y_{N_1}.
  • One line with one integer N2N_2 (2≤N2≤25)(2 \le N_2 \le 25), the number of points describing the right wall of the mineshaft.
  • N2N_2 lines with the points describing the right wall, from the bottom to the top, in the same format, with y1′<y2′<⋯<yN2′y'_1 < y'_2 < \cdots < y'_{N_2}.

Every test case satisfies x1<x1′x_1 < x'_1, y1=y1′y_1 = y'_1, xN1<xN2′x_{N_1} < x'_{N_2} and yN1=yN2′y_{N_1} = y'_{N_2}. The two walls never cross and never touch.

Output

For every test case, print one line with a single number: the minimum number of bends in a pipeline that runs from the bottom of the mineshaft to the top under the conditions above.

Hint

The test cases below correspond to the pictures in the statement.

Examples1

  1. Example 1

    Input
    2
    7
    4 0
    8 2
    3 7
    5 9
    3 12
    6 14
    4 16
    5
    7 0
    10 2
    6 6
    10 12
    8 16
    6
    -10 10
    -10 20
    10 22
    5 27
    0 37
    5 47
    8
    -8 10
    -8 16
    3 17
    9 19
    14 24
    18 32
    18 40
    11 47
    
    Expected output
    2
    3