Fabled Rooks

We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to the following restrictions • The i-th rook can only be placed within the rectangle given by its left-upper corner (xli,yli) and its right-lower corner (xri,yri),where1≤i≤n,1≤xli ≤xri ≤n,1≤yli ≤ yri ≤ n. • No two rooks can attack each other, that is no two rooks can occupy the same column or the same row. Input The input consists of several test cases. The first line of each of them contains one integer number, n, the side of the board. n lines follow giving the rectangles where the rooks can be placed as described above. The i-th line among them gives xli, yli, xri, and yri. The input file is terminated with the integer ‘0’ on a line by itself. Output Your task is to find such a placing of rooks that the above conditions are satisfied and then output n lines each giving the position of a rook in order in which their rectangles appeared in the input. If there are multiple solutions, any one will do. Output ‘IMPOSSIBLE’ if there is no such placing of the rooks. Sample Input 8 1122 5788 2255 2255 6386 6385 6388 3678 8 1122 5788 2255 2255 6386 6385 6388 3678 0

2/2 Sample Output 11 58 24 42 73 85 66 37 11 58 24 42 73 85 66 37