This page is still under construction.

Parts of this page are still being built. What you see may change.

Transforming Comets

Time limit5sMemory limit512 MB

Summary
Given two cyclic sequences of integer points, decide whether one is a rotation, uniform positive scaling, and translation of the other, and report the matching cyclic offset.
Level

Hard8 of 10

Topics
String matching, Geometry, String, Math
Solved
No attempts yet

Problem

While traveling from Earth to Krypton, Superman was caught in a wormhole and instantly transported to an unknown location. He remembers the periodic comets he used to watch from Earth, and from his new position he can also see some periodic comets. He would like to use them to get his bearings, but first he must work out which comet is which.

These comets are periodic Gaussian hyper-comets. A periodic Gaussian hyper-comet is a sequence (p1,p2,…,pn)(p_1, p_2, \ldots, p_n) where each pi=(xi,yi)p_i = (x_i, y_i) is a point with integer coordinates. The comet visits pip_i and then pi+1p_{i+1}; the sequence is periodic, so after pnp_n it visits p1p_1 again (indices are taken modulo nn). A Gaussian hyper-comet also satisfies pi≠pi+1p_i \neq p_{i+1} for every ii, and p1≠pnp_1 \neq p_n.

Superman was disoriented in both space and time. In space, a comet he once knew may now appear rotated, scaled uniformly by the same positive factor on both axes, and/or translated. In time, the point he remembers as the first point may no longer be listed first.

For example, the right-triangular hyper-comet ((0,0),(1,0),(0,1))((0,0),(1,0),(0,1)) seen from Earth might now appear as ((40,40),(20,20),(60,20))((40,40),(20,20),(60,20)) or as ((20,20),(60,20),(40,40))((20,20),(60,20),(40,40)). Reversing space or time is not allowed: this comet can never appear as ((0,1),(1,0),(0,0))((0,1),(1,0),(0,0)).

Given one Gaussian hyper-comet as seen from Earth and one as seen from Superman's current location, decide whether they could be the same comet.

Input

The first line contains an integer tt (1≤t≤101 \le t \le 10), the number of test cases.

Each test case begins with an integer nn (2≤n≤500 0002 \le n \le 500\,000). The next nn lines each contain two space-separated integers xi yix_i\ y_i (i=1…ni = 1 \ldots n), the points of the comet seen from Earth. Then nn more lines follow, each containing two space-separated integers xi′ yi′x'_i\ y'_i, the points of the comet seen from Superman's current location.

All coordinates are integers between 00 and 30 00030\,000 inclusive. For each comet, pi≠pi+1p_i \neq p_{i+1} for every ii and p1≠pnp_1 \neq p_n.

Output

For each test case, print the smallest positive integer ss such that the Earth point p1=(x1,y1)p_1 = (x_1, y_1) can correspond to the current point qs=(xs′,ys′)q_s = (x'_s, y'_s) under the disorientation described above (rotation, uniform scaling, translation, and a cyclic time shift, with no reflection and no time reversal). If the two comets cannot be the same, print 00.

Examples1

  1. Example 1

    Input
    3
    3
    0 0
    1 0
    0 1
    20 20
    60 20
    40 40
    4
    0 0
    1 1
    0 0
    1 1
    30 30
    19 23
    30 30
    19 23
    4
    0 0
    1 0
    1 1
    0 1
    0 0
    2 0
    2 1
    0 1
    
    Expected output
    3
    1
    0