This page is still under construction.

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

Decorate the Wall

Interview

Time limit1sMemory limit128 MB

Summary
Given non-overlapping axis-aligned rectangles on a wall, find the lowest then leftmost position where a new w' by h' rectangle fits without overlapping any of them, or report failure.
Level

Medium6 of 10

Topics
Geometry, Sorting, Binary search, Intervals
Solved
No attempts yet

Problem

Mr. Rich has just finished building his enormous villa, and the bare interior walls bother him. He decides to hang paintings from his collection, but it quickly becomes hard to find a spot on a wall where a new painting fits without overlapping the ones already hanging.

Write a program that, given the paintings already on a wall, decides where to hang the next painting without moving any existing painting — or reports that it is impossible.

Every painting is an axis-aligned rectangle whose sides are parallel to the edges of the wall, and paintings may not be rotated.

Input

The first line contains the number of test cases.

Each test case begins with a line containing three integers nn, ww, and hh: the number of paintings already on the wall, the width of the wall, and the height of the wall.

Each of the next nn lines contains four integers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2 with 0≤x1<x2≤w0 \le x_1 < x_2 \le w and 0≤y1<y2≤h0 \le y_1 < y_2 \le h. The xx-coordinates measure the distance from the left edge of the wall and the yy-coordinates measure the distance from the bottom edge. (x1,y1)(x_1, y_1) is the lower-left corner of a painting and (x2,y2)(x_2, y_2) is its upper-right corner.

The last line of the test case contains the size of the next painting to hang: its width w′w' followed by its height h′h' (1≤w′≤w1 \le w' \le w, 1≤h′≤h1 \le h' \le h). The painting may not be rotated.

You may assume 0≤n≤2000 \le n \le 200 and 1≤w,h≤10000001 \le w, h \le 1000000. The paintings already on the wall never overlap one another.

Output

For each test case print one line.

If there is no free spot where the new painting fits without overlapping any existing painting, print Fail!.

Otherwise print the coordinates of the lower-left corner where the painting should be placed, as two integers x y separated by a single space. Two paintings that only touch along an edge or at a corner do not count as overlapping. When more than one placement is possible, choose the one with the smallest yy; if several placements share that smallest yy, choose the one with the smallest xx.

Examples2

  1. Example 1

    Input
    2
    1 10 9
    5 4 10 9
    9 5
    2 10 10
    5 5 10 10
    0 0 4 3
    3 4
    
    Expected output
    Fail!
    4 0
    
  2. Example 2

    Input
    1
    0 5 5
    2 2
    
    Expected output
    0 0