The N Queens Puzzle Revisited

All submissions for this problem are available.
Johnny, a computer science student from the Bytelandian Technological University (BTU), has finally managed to write a program to solve the N queens problem. However, his version of the N queens problem is not really all that similar to the classical one.
Recall that in the N queens problem, there is a square chessboard of size NxN. The goal is to put N queens on the chessboard so that no two queens would be able to attack each other.
In Johnny's version of the problem (which he states to be "much harder" than the original one), there are K "blocked" squares given on the chessboard. The goal is still the same: to put N queens on the chessboard such that no two queens would be able to attack each other. However, there are some differences due to the existence of blocked squares. First, a queen cannot be put on a blocked square. Secondly, two queens are considered to be able to attack each other only if there is no blocked square between them. Finally, there must be exactly one queen on every column of the board.
Johnny claims that his program solves the above problem very effectively. He generates some test cases (which his program can solve within minutes) to challenge you. You know the method Johnny used to generate the test case. For a given N, Johnny randomly picks a number K between 1 and N^{2}/2 with equal probability. After that, Johnny picks K blocked squares on the chess board. Any combinations of K squares can be picked with equal probability.
Write a program that can solve Johnny's test cases within seconds.
Input
The first line contains t, the number of test cases (about 5). Then t test cases follow. Each test case has the following form:
 The first line contains two integers N and K (8 <= N <= 500, 1 <= K <= N^{2}/2)
 Each line in the next K lines contains two integers i, j which are the row and the column number of a blocked square (1 <= i, j <= N)
Each test case's input is separated by a blank line. The rows and the columns of the chessboard are numbered from 1 to N. There is always a solution for each test case.
Output
For each test case, print a single line containing N integers a_{1}, a_{2}, ..., a_{N} in which a_{i} is the row number of the queen in column i. Any correct solution will be accepted.
Example
Input 1 8 15 7 7 3 1 7 1 4 8 8 3 6 5 4 4 2 1 8 2 6 3 3 7 7 6 2 3 1 2 6 4 Output 4 2 7 5 2 4 6 8
Author:  admin 
Editorial  http://discuss.codechef.com/problems/J3 
Tags  admin 
Date Added:  15102009 
Time Limit:  0.144615  1.87 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, TEXT, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 