Rectilinear polygon

Given is n points with integer coordinates in the plane. Is it is possible to construct a simple, that is non-intersecting, recti- linear polygon with the given points as vertices? In a rectilinear polygon there are at least 4 vertices and every edge is either horizontal or vertical; each vertex is an endpoint of exactly one horizontal edge and one vertical edge. There are no holes in a polygon. Input The first line of input is an integer giving the number of cases that follow. The in- put of each case starts with an integer 4 ≤ n ≤ 100000 giving the number of points for this test case. It is followed by n pairs of integers specifying the x and y coordinates of the points for this case. Output The output should contain one line for each case on input. Each line should contain one integer number giving the length of the rectilinear polygon passing throught the given points when it exists; otherwise, it should contain ‘-1’. Sample Input 1 8 12 10 21 22 32 31 40 42 Sample Output 12