Under construction

Still being built. Things may change

K-Shaped Figures

Time limit3sMemory limit1024 MB

Summary
Count triples of segments where two share an endpoint lying strictly inside the third segment, with both on the same side of it.
Level

Medium7 of 10

Topics
Geometry, Sorting, Hash map
Solved
No attempts yet

Problem

Time limit: 3000 ms. Memory limit: 1024 MB.

Let's say that three segments on a plane form a K-shaped figure if:

  • two of them share a common endpoint;
  • this common endpoint lies strictly inside the third segment;
  • these two segments are located on the same side with respect to the third one;
  • all three segments are pairwise not collinear.
{AB,CD,CE}\{AB,CD,CE\}{AB,CD,CE}\{AB,CD,CE\}{AB,CD,CE}\{AB,CD,CE\}{AB,CD,CE}\{AB,CD,CE\}{AB,CD,CE}\{AB,CD,CE\}{AB,CD,EF}\{AB,CD,EF\}{AB,AC,AD}\{AB,AC,AD\}
Valid K-shaped figuresInvalid K-shaped figures

You are given a collection of nn segments on the plane. Find the number of triples of segments from this collection that form a K-shaped figure.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤33331 \le t \le 3333). The description of the test cases follows.

The first line of each test case contains a single integer nn, the number of segments (3≤n≤10003 \le n \le 1000).

The ii-th of the following nn lines contains four integers xi,1x_{i,1}, yi,1y_{i,1}, xi,2x_{i,2}, yi,2y_{i,2}, the coordinates of the endpoints of the ii-th segment (−106≤xi,1,yi,1,xi,2,yi,2≤106-10^6 \le x_{i,1}, y_{i,1}, x_{i,2}, y_{i,2} \le 10^6). All segments have positive lengths. Some segments may coincide.

It is guaranteed that the sum of nn over all test cases does not exceed 10410^4.

Output

For each test case, print a single integer: the number of triples of segments that form a K-shaped figure.

Examples1

  1. Example 1

    Input
    2
    5
    0 0 0 10
    0 5 3 10
    0 5 3 0
    0 5 7 4
    0 5 6 2
    8
    0 0 10 10
    3 4 4 4
    4 4 4 5
    3 4 4 4
    7 7 7 8
    7 7 8 7
    5 5 4 6
    5 5 3 7
    
    Expected output
    6
    2