def is_safe(row, col, queens):
    for r in range(row):
        c = queens[r]
        # Same column conflict
        if c == col:
            return False
        # Diagonal conflict
        if abs(row - r) == abs(col - c):
            return False
            
    return True

def backtrack(row, n, queens, sols):
    if row == n:
        sols.append(list(queens))
        return

    for col in range(n):
        if is_safe(row, col, queens):
            queens[row] = col
            
            backtrack(row + 1, n, queens, sols)            
            queens[row] = -1

def print_solution(solution, n, sol_num):
    print(f"Solution {sol_num}:")
    for r in range(n):
        line = []
        for c in range(n):
            if solution[r] == c:
                line.append("Q")
            else:
                line.append(".")
        print(" ".join(line))
    print()

def solve():
    try:
        line = input()
        if not line:
            return
        n = int(line.strip())
    except (EOFError, ValueError):
        return

    queens = [-1] * n
    sols = []
    
    backtrack(0, n, queens, sols)
    
    if not sols:
        print("No solution exists")
    else:
        for i, sol in enumerate(sols):
            print_solution(sol, n, i + 1)

if __name__ == "__main__":
    solve()

# Input:
# 4

# Output:
# Solution 1:
# . Q . .
# . . . Q
# Q . . .
# . . Q .
#
# Solution 2:
# . . Q .
# Q . . .
# . . . Q
# . Q . .