def put_queen(i)
  if i == N
    @row.each do |x|
      N.times do |i|
        print (x == i) ? "Q" : "."
      end
      puts
    end
    return
  end

  unless @input_set_queen.include?(i)
    N.times do |j|
      if @col[j] == NOT_FREE || @dpos[i+j] == NOT_FREE || @dneg[i-j+N-1] == NOT_FREE
        next
      end
      # put a queen at (i, j)
      @row[i] = j
      @col[j] = @dpos[i+j] = @dneg[i-j+N-1] = NOT_FREE
      # try the next row
      put_queen(i+1)
      #remove the queen at (i, j) for backtracking
      @col[j] = @dpos[i+j] = @dneg[i-j+N-1] = FREE
    end
  else
    put_queen(i+1)
  end
end



N = 8
FREE = "O"
NOT_FREE = "X"

@row = Array.new(N)
@col = Array.new(N,FREE)
@dpos = Array.new(N*2-1,FREE)
@dneg = Array.new(N*2-1,FREE)
@input_set_queen = []

gets.to_i.times do
  r, c = gets.split(" ").map{|x| x.to_i}
  @input_set_queen << r
  @row[r] = c
  @col[c] = @dpos[r+c] = @dneg[r-c+N-1] = NOT_FREE
end

put_queen(0)

