
import sys
def main(N):
    lines = []
    # --- Row constraints: one queen per row ---
    for r in range(N):
        # At least one queen in row r
        row_vars = [f"q_{r}_{c}" for c in range(N)]
        lines.append(" | ".join(row_vars))
        
        # At most one queen in row r (pairwise exclusion)
        for c1 in range(N):
            for c2 in range(c1 + 1, N):
                lines.append(f"!q_{r}_{c1} | !q_{r}_{c2}")

    # --- Column constraints: one queen per column ---
    for c in range(N):
        col_vars = [f"q_{r}_{c}" for r in range(N)]
        lines.append(" | ".join(col_vars))
        
        # At most one queen in column c
        for r1 in range(N):
            for r2 in range(r1 + 1, N):
                lines.append(f"!q_{r1}_{c} | !q_{r2}_{c}")

    # --- Diagonal constraints ---
    for r1 in range(N):
        for c1 in range(N):
            for r2 in range(N):
                for c2 in range(N):
                    if (r1, c1) < (r2, c2):  # avoid duplicates
                        if abs(r1 - r2) == abs(c1 - c2):  # same diagonal
                            lines.append(f"!q_{r1}_{c1} | !q_{r2}_{c2}")


    # first add parentheses for clarity
    lines = [f"({line})" for line in lines]
    print("\n & ".join(lines))

if __name__ == "__main__":
    N = int(sys.argv[1]) if len(sys.argv) > 1 else 8
    main(N)