#include <bits/stdc++.h>
using namespace std;

int n;
char board[20][20]; // supports up to n=20

// Check if placing queen at board[row][col] is safe
bool isSafe(int row, int col) {
    int i, j;

    // Check column above
    for (i = 0; i < row; i++)
        if (board[i][col] == 'Q')
            return false;

    // Check upper-left diagonal
    for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
        if (board[i][j] == 'Q')
            return false;

    // Check upper-right diagonal
    for (i = row, j = col; i >= 0 && j < n; i--, j++)
        if (board[i][j] == 'Q')
            return false;

    return true;
}

// Solve N-Queens problem
bool solveNQueens(int row) {
    if (row == n) {
        // All queens placed, print solution
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << board[i][j] << " ";
            }
            cout << "\n";
        }
        cout << "\n";
        return true; // at least one solution
    }

    bool res = false;
    for (int col = 0; col < n; col++) {
        if (isSafe(row, col)) {
            board[row][col] = 'Q';          // place queen
            res = solveNQueens(row + 1) || res;
            board[row][col] = ".";          // backtrack
        }
    }
    return res;
}

int main() {
   

    cin >> n;

    // initialize board
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            board[i][j] = 0;

    if (!solveNQueens(0)) {
        cout << "No solution exists\n";
    }

    return 0;
}