This page is still under construction.

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

Bridge

Time limit1sMemory limit128 MB

Summary
Connect all axis-parallel rectangular islands with bridges of minimum total squared shortest gap distance.
Level

Medium5 of 10

Topics
Minimum spanning tree, Geometry
Solved
No attempts yet

Problem

Recently a large typhoon swept away every bridge between the islands. What is done is done, and now the road network has to be rebuilt. As an emergency recovery plan, the government decided to build bridges so that people can travel between any pair of islands without getting wet. In other words, all islands must end up connected to one another through bridges.

Building a bridge of length xx costs x2x^2. The length of a bridge between two islands is the shortest straight-line distance between them, that is, the Euclidean distance between the two closest points of the two rectangles. You may build as many bridges as you like; connect all islands so that every island is reachable from every other one, while keeping the total cost as small as possible.

Thanks to land reclamation, every island is a rectangle whose sides are parallel to the coordinate axes. Given the positions and shapes of the islands, compute the minimum total cost of connecting them all.


Figure 1. An example of connecting islands with bridges.

Input

The first line contains an integer TT with 1≤T≤201 \le T \le 20, the number of test cases.

Each test case begins with a line containing an integer NN with 2≤N≤50002 \le N \le 5000, the number of islands. Each of the next NN lines contains four integers xx, yy, ww, hh with 0≤x,y,w,h≤100000 \le x, y, w, h \le 10000, where (x,y)(x, y) is the upper-left corner of an island and ww and hh are its width and height. The island covers every point (p,q)(p, q) with x≤p≤x+wx \le p \le x + w and y−h≤q≤yy - h \le q \le y.

Output

For each test case, print a single line with one integer: the minimum total cost of connecting all islands.

Examples1

  1. Example 1

    Input
    2
    2
    1 1 5 1
    2 3 1 1
    4
    6 4 3 4
    2 8 6 3
    1 14 4 4
    9 12 4 3
    
    Expected output
    1
    7