Under construction

Still being built. Things may change

Sail Shreds 4

Time limit2sMemory limit512 MB

Summary
Translate each triangle without rotation so that the triangles exactly tile an X by Y rectangle, with no overlaps and no part outside.
Level

Hard8 of 10

Topics
Geometry, Greedy, Sorting
Solved
No attempts yet

Problem

During the recent two-day storm, a group of sailors shipwrecked on a desert island in the Mediterranean sea. Since their radio is broken, they cannot call for help and their only chance of survival is escaping on a raft. However, they need a sail to power the raft. Luckily, they recovered the main sail from the wrecked ship, but it is torn to several shreds. Hence, they have to sew the shreds together to obtain the original sail.

The sailors quickly realized that each of the shreds is a triangle. Because there were stripes on the sail, it was easy to identify the correct orientation of its triangular parts. The shreds must keep their orientations. Otherwise the sail would be less resistant to wind and might be torn again. They spent several hours looking for the original arrangement without success. They think a computer can help, so they asked you.

You are given the plane coordinates of the vertices of each triangular shred and the dimensions of the original sail, which was a rectangle given by two integers XX and YY. Your task is to move the shreds (without rotation) to reconstruct the original sail (see the figure). In particular, the triangles must be moved so that:

  • the whole rectangular area is covered,
  • no two triangles intersect, that is, no point lies in the interiors of two different triangles, and
  • no triangle has any part outside the rectangle.

You may assume that a solution exists for each input. In particular, the sum of the areas of the shreds equals the area of the original sail. Several shreds may have the same shape.

Input

The first line contains three integers NN, XX, and YY, separated by single spaces. XX is the width and YY is the height of the original sail, with 1≤X,Y≤10000001 \le X, Y \le 1000000. NN is the number of triangular shreds, with 2≤N≤10002 \le N \le 1000.

The next NN lines describe the triangles. The (i+1)(i+1)-th line contains six integers Ai,xA_{i,x}, Ai,yA_{i,y}, Bi,xB_{i,x}, Bi,yB_{i,y}, Ci,xC_{i,x}, and Ci,yC_{i,y}, separated by single spaces. These describe the three vertices AiA_i, BiB_i, and CiC_i of the ii-th triangle, where AiA_i is at (Ai,x,Ai,y)(A_{i,x}, A_{i,y}), BiB_i is at (Bi,x,Bi,y)(B_{i,x}, B_{i,y}), and CiC_i is at (Ci,x,Ci,y)(C_{i,x}, C_{i,y}). All coordinates satisfy 0≤0 \le coordinate ≤1000000\le 1000000.

The position of a triangle in the input is irrelevant. Translating a triangle to another position gives the same set of shreds.

Output

Output exactly NN lines. The ii-th line contains two integers Ai,x∗A^*_{i,x} and Ai,y∗A^*_{i,y}, separated by a single space. These are the coordinates of the point AiA_i after the ii-th shred is moved to its place. All three conditions in the problem must hold. The lower left corner of the sail is at (0,0)(0, 0) and the upper right corner is at (X,Y)(X, Y), as shown in the figure.

Examples1

  1. Example 1

    Input
    2 2 2
    0 0 2 0 0 2
    0 0 2 0 0 2
    
    Expected output
    0 0
    0 2