G. Manhattan Triangle, Euclidean Square
time limit per test
5 seconds
memory limit per test
4 megabytes
input
standard input
output
standard output

Please note the unusual memory limit.

You are given $$$n$$$ distinct points $$$P_1,P_2,\ldots,P_n$$$ on a plane. The distance $$$\text{dist}(p_1,p_2)$$$ between two points $$$p_1=(x_1,y_1)$$$ and $$$p_2=(x_2,y_2)$$$ is defined as the Manhattan distance $$$|x_1-x_2|+|y_1-y_2|$$$.

Three points $$$p_i=(x_i,y_i)$$$, $$$p_j=(x_j,y_j)$$$, $$$p_k=(x_k,y_k)$$$ are said to form a Manhattan Triangle in Square if the following conditions hold.

  • The distances between any two points are equal. In other words, $$$\text{dist}(p_i,p_j)$$$, $$$\text{dist}(p_i,p_k)$$$, $$$\text{dist}(p_j,p_k)$$$ are equal.
  • The ranges of $$$x$$$ and $$$y$$$ form a square. In other words, $$$\max(x_i,x_j,x_k)-\min(x_i,x_j,x_k)$$$ and $$$\max(y_i,y_j,y_k)-\min(y_i,y_j,y_k)$$$ are equal.
An example of three points forming a Manhattan Triangle in Square.

Given the coordinates of the points, please count the triples $$$(i,j,k)$$$ that satisfy the following conditions.

  • $$$1 \le i \lt j \lt k \le n$$$;
  • The points $$$P_i$$$, $$$P_j$$$, $$$P_k$$$ form a Manhattan Triangle in Square.
Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ — the number of points ($$$1 \le n \le 20\,000$$$).

Each of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ denoting the point $$$P_i$$$ ($$$-10^7 \le x_i,y_i \le 10^7$$$).

It is guaranteed that the given points are pairwise distinct.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$20\,000$$$.

Output

For each test case, output the number of triples that satisfy the given conditions on a separate line.

Example
Input
2
3
0 0
6 2
2 6
20
0 0
0 1
0 2
0 3
0 4
1 1
1 2
1 3
1 4
1 5
2 0
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
3 5
Output
1
8
Note

The first test case has one triple forming a Manhattan Triangle in Square, identical to the one given in the diagram above.

It can be shown that the second test case has exactly $$$8$$$ triples forming a Manhattan Triangle in Square.