#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 10000
char stack[MAX_SIZE][10];
int top = -1;
void push(char* token) {
    strcpy(stack[++top], token);
}
char* pop() {
    if (top >= 0) {
        return stack[top--];
    }
    return NULL;
}
char* peek() {
    if (top >= 0) {
        return stack[top];
    }
    return NULL;
}
int getPrecedence(char* op) {
    if (strcmp(op, "NOT") == 0) return 3;
    if (strcmp(op, "AND") == 0) return 2;
    if (strcmp(op, "OR") == 0) return 1;
    return 0;
}
int isOperator(char* token) {
    return (strcmp(token, "AND") == 0 || strcmp(token, "OR") == 0 || strcmp(token, "NOT") == 0);
}
int isVariable(char* token) {
    return (strlen(token) == 1 && token[0] >= 'A' && token[0] <= 'Z');
}
int validateParentheses(char* expression) {
    int count = 0;
    for (int i = 0; expression[i] != '\0'; i++) {
        if (expression[i] == '(') count++;
        else if (expression[i] == ')') {
            count--;
            if (count < 0) return 0; 
    }
    return count == 0; 
}
int main() {
    char expression[MAX_SIZE];
    char result[MAX_SIZE] = "";
    char token[10];
    int i = 0, j = 0;
    int firstOutput = 1;
    if (fgets(expression, sizeof(expression), stdin) == NULL) {
        printf("Invalid input\n");
        return 0;
    }
    int len = strlen(expression);
    if (len > 0 && expression[len-1] == '\n') {
        expression[len-1] = '\0';
        len--;
    }
    if (len < 1 || len > 10000) {
        printf("Invalid input\n");
        return 0;
    }
    if (!validateParentheses(expression)) {
        printf("Invalid input\n");
        return 0;
    }
    while (i < len) {
        j = 0;
        while (i < len && expression[i] == ' ') {
            i++;
        }
        if (i >= len) break;
        if (expression[i] == '(' || expression[i] == ')') {
            token[j++] = expression[i++];
            token[j] = '\0';
        }
        else {
            while (i < len && expression[i] != ' ' && expression[i] != '(' && expression[i] != ')') {
                token[j++] = expression[i++];
            }
            token[j] = '\0';
        }
        if (strlen(token) == 0) continue;
        if (isVariable(token)) {
            if (!firstOutput) {
                strcat(result, " ");
            }
            strcat(result, token);
            firstOutput = 0;
        }
        else if (strcmp(token, "(") == 0) 
            push(token);
        }
        else if (strcmp(token, ")") == 0) 
            char* topOp = pop();
            while (topOp != NULL && strcmp(topOp, "(") != 0) {
                if (!firstOutput) {
                    strcat(result, " ");
                }
                strcat(result, topOp);
                firstOutput = 0;
                topOp = pop();
            }
            if (topOp == NULL) {
                printf("Invalid input\n");
                return 0;
            }
        }
        else if (isOperator(token)) {
            while (peek() != NULL && strcmp(peek(), "(") != 0 && 
                   getPrecedence(peek()) >= getPrecedence(token)) {
                char* topOp = pop();
                if (!firstOutput) {
                    strcat(result, " ");
                }
                strcat(result, topOp);
                firstOutput = 0;
            }
            push(token);
        }
        else {
            printf("Invalid input\n");
            return 0;
        }
    }
    while (peek() != NULL) {
        char* topOp = pop();
        if (strcmp(topOp, "(") == 0 || strcmp(topOp, ")") == 0) {
            printf("Invalid input\n");
            return 0;
        }
        if (!firstOutput) {
            strcat(result, " ");
        }
        strcat(result, topOp);
        firstOutput = 0;
    }
    printf("%s\n", result);
    return 0;
}