This page is still under construction.

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

Block Compaction

Time limit1sMemory limit128 MB

Summary
Repeatedly drop axis-aligned rectangles down and then left until none moves, and report the width and height of the final bounding box.
Level

Hard8 of 10

Topics
Simulation, Geometry, Sorting, Intervals
Solved
No attempts yet

Problem

You are given a set RR of rectangles. Every rectangle is parallel to the xx and yy axes, and the rectangles are mutually disjoint, although they may touch along boundary edges. All rectangles lie in the quadrant x≥0x \ge 0, y≥0y \ge 0.

Compact the rectangles with the procedure COMPACT below. Once the procedure finishes, the position of every rectangle is fixed. Your task is to find the smallest enclosing rectangle that contains all of them. The enclosing rectangle must also be parallel to the xx and yy axes.

do {
     Step 1. Move blocks downward until no blocks can be moved.
     Step 2. Move blocks leftward until no blocks can be moved.
} Until no blocks can be moved downward or leftward.

The procedure works as follows. Figure 1 is the initial layout of the given rectangles. After moving the blocks downward as far as possible, we obtain the layout in Figure 2.

Figure 1. Input rectangles

Figure 2. After moving the blocks downward

No block can move downward any further in Figure 2, so we move the blocks leftward and obtain Figure 3. Throughout COMPACT the blocks must always stay mutually disjoint, but they may be packed together so that they share boundary edges. See how the groups {E, G, F}, {A, B}, and {C, D} in Figure 2 share boundary edges.

Figure 3. After moving the blocks leftward

Figure 4. After moving the blocks downward again

Repeating the procedure as in Figure 4 until no block can move gives the final compacted layout in Figure 5. The dotted rectangle is the smallest enclosing rectangle.

Figure 5. Applying COMPACT until no block can move yields the smallest enclosing rectangle (the dotted box).

Compute the final enclosing rectangle produced by COMPACT.

Input

Read from standard input. The input consists of TT test cases, and the first line contains TT. The first line of each test case contains NN (1≤N≤5001 \le N \le 500), the number of rectangles. Each of the next NN lines describes one rectangle by the integer coordinates of its lower-left vertex (x,y)(x, y) and its upper-right vertex (p,q)(p, q), given on a single line as x y p q, where x<px < p, y<qy < q, and 0≤x,y,p,q≤1000000 \le x, y, p, q \le 100000.

Output

Write to standard output. For each test case, print exactly one line with two numbers WW and HH: the width and the height of the enclosing rectangle produced by COMPACT.

Examples1

  1. Example 1

    Input
    3
    3
    10 10 30 30
    10 50 15 55
    90 10 100 100
    4
    10001 1001 11001 70001
    20001 15001 25001 30001
    20001 40001 80001 45001
    20000 60000 28500 61000
    1
    34010 34010 34100 34100
    
    Expected output
    30 90
    61000 69000
    90 90