Origami Axiom Six: Counting Folds

Time limit1sMemory limit512 MB

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 $p_1$ and $p_2$, there is a unique fold passing through both.
  2. Given points $p_1$ and $p_2$, there is a unique fold that places $p_1$ onto $p_2$.
  3. Given lines $l_1$ and $l_2$, there is a fold that places $l_1$ onto $l_2$.
  4. Given a point $p_1$ and a line $l_1$, there is a unique fold perpendicular to $l_1$ that passes through $p_1$.
  5. Given points $p_1$, $p_2$ and a line $l_1$, there is a fold that places $p_1$ onto $l_1$ and passes through $p_2$.
  6. Given points $p_1$, $p_2$ and lines $l_1$, $l_2$, there is a fold that places $p_1$ onto $l_1$ and $p_2$ onto $l_2$.

The sixth axiom is the hard one: a single straight fold must simultaneously reflect $p_1$ onto line $l_1$ and $p_2$ onto line $l_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 $p_1$ onto $l_1$ and $p_2$ onto $l_2$ at the same time.

Input

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

Each test case is given on exactly four lines, describing $l_1$, $p_1$, $l_2$ and $p_2$ in that order:

  • a line is given by four integers $x_1\ y_1\ x_2\ y_2$, the coordinates of two distinct points on it;
  • a point is given by two integers $x\ y$.

All coordinates are integers with absolute value at most $10$. It is guaranteed that $p_1$ does not lie on $l_1$ and $p_2$ does not lie on $l_2$. The lines $l_1$ and $l_2$ are different, but the points $p_1$ and $p_2$ may coincide.

Output

For each test case output a single line containing one integer: the number of distinct fold lines that place $p_1$ onto $l_1$ and $p_2$ onto $l_2$. This value is always $0$, $1$, $2$, or $3$.

Notes

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