Radar Scanner

Time limit2sMemory limit512 MB

Summary
Given n axis-aligned rectangles, count triples whose common intersection is nonempty.
Level

Medium7 of 10

Topics
Geometry, Intervals, Combinatorics, Implementation
Solved
No attempts yet

Problem

There are n rectangular radar scanners on the plane. Their sides are all parallel to the coordinate axes. Each scanner covers some grid squares on the plane. The i-th scanner covers all squares (x,y)(x, y) satisfying xi,1≤x≤xi,2x_{i,1} \le x \le x_{i,2} and yi,1≤y≤yi,2y_{i,1} \le y \le y_{i,2}.

Today, the radar system is facing a critical low-power problem. To reduce coverage, you must choose exactly three scanners, and there must be at least one square covered by all three chosen scanners.

Find the number of tuples (i,j,k)(i, j, k) such that 1≤i<j<k≤n1 \le i < j < k \le n and there exists a square covered by all of scanners i, j, and k.

Input

The first line of the input contains an integer T (1 ≤ T ≤ 10), the number of test cases.

Each test case starts with a line containing an integer n (3 ≤ n ≤ 100 000), the number of radar scanners.

Each of the next n lines contains four integers xi,1x_{i,1}, yi,1y_{i,1}, xi,2x_{i,2}, and yi,2y_{i,2} (1 ≤ xi,1x_{i,1} ≤ xi,2x_{i,2} ≤ 1000, 1 ≤ yi,1y_{i,1} ≤ yi,2y_{i,2} ≤ 1000), describing the i-th radar scanner.

Output

For each test case, print a single line containing a single integer: the number of possible tuples.

Examples1

  1. Example 1

    Input
    2
    3
    3 1 3 1
    1 1 2 3
    2 1 3 2
    5
    1 1 4 5
    2 1 3 2
    2 2 3 3
    4 5 4 5
    1 2 2 4
    
    Expected output
    0
    4