Origami Axiom Six: Counting Folds

Time limit1sMemory limit512 MB

Summary
Given two point-line pairs, count the distinct fold lines (common tangents of two parabolas) satisfying Huzita's sixth origami axiom for up to 20000 test cases.
Level

Hard9 of 10

Topics
Geometry, Math, Number theory
Solved
No attempts yet

Problem

The first formal set of axioms for origami was published by Humiaki Huzita and Benedetto Scimemi and is known as the Huzita axioms. Each axiom describes a way in which a single fold line can be produced by aligning points and lines. One version of the six axioms is:

  1. Given points p1p_1 and p2p_2, there is a unique fold passing through both.
  2. Given points p1p_1 and p2p_2, there is a unique fold that places p1p_1 onto p2p_2.
  3. Given lines l1l_1 and l2l_2, there is a fold that places l1l_1 onto l2l_2.
  4. Given a point p1p_1 and a line l1l_1, there is a unique fold perpendicular to l1l_1 that passes through p1p_1.
  5. Given points p1p_1, p2p_2 and a line l1l_1, there is a fold that places p1p_1 onto l1l_1 and passes through p2p_2.
  6. Given points p1p_1, p2p_2 and lines l1l_1, l2l_2, there is a fold that places p1p_1 onto l1l_1 and p2p_2 onto l2l_2.

The sixth axiom is the hard one: a single straight fold must simultaneously reflect p1p_1 onto line l1l_1 and p2p_2 onto line l2l_2. Depending on the configuration there may be no such fold, or one, two, or three of them.

For each test case, determine how many distinct fold lines satisfy the sixth axiom — that is, how many distinct straight lines reflect p1p_1 onto l1l_1 and p2p_2 onto l2l_2 at the same time.

Input

The first line contains the number of test cases tt (1≤t≤200001 \le t \le 20000).

Each test case is given on exactly four lines, describing l1l_1, p1p_1, l2l_2 and p2p_2 in that order:

  • a line is given by four integers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2, the coordinates of two distinct points on it;
  • a point is given by two integers x yx\ y.

All coordinates are integers with absolute value at most 1010. It is guaranteed that p1p_1 does not lie on l1l_1 and p2p_2 does not lie on l2l_2. The lines l1l_1 and l2l_2 are different, but the points p1p_1 and p2p_2 may coincide.

Output

For each test case output a single line containing one integer: the number of distinct fold lines that place p1p_1 onto l1l_1 and p2p_2 onto l2l_2. This value is always 00, 11, 22, or 33.

Notes

A fold that reflects a point pp onto a line ll is exactly a tangent line to the parabola whose focus is pp and whose directrix is ll. A fold satisfying the sixth axiom is therefore a common tangent of the parabola (p1,l1)(p_1, l_1) and the parabola (p2,l2)(p_2, l_2). Two distinct parabolas share at most three ordinary common tangents, which is why the answer never exceeds three. When l1l_1 and l2l_2 are parallel the number of common tangents can drop to two, one, or zero.

Examples4

  1. Example 1

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

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

    Input
    1
    1 0 -1 -2
    4 -2
    -2 -2 5 5
    0 4
    
    Expected output
    2
    
  4. Example 4

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