#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 10005

int precedence(const 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 main() {
    char expr[MAX];
    if (!fgets(expr, sizeof(expr), stdin)) {
        printf("Invalid input\n");
        return 0;
    }
    int len = strlen(expr);
    if (expr[len-1] == '\n') expr[--len] = '\0';
    char stack[MAX][5];
    int top = -1;
    int valid = 1, paren = 0;

    for (int i = 0; i < len && valid;) {
        if (expr[i] == ' ') {
            i++;
            continue;
        }
        if (expr[i] == '(') {
            strcpy(stack[++top], "(");
            paren++;
            i++;
        } else if (expr[i] == ')') {
            paren--;
            while (top >= 0 && strcmp(stack[top], "(") != 0) {
                printf("%s ", stack[top--]);
            }
            if (top >= 0 && strcmp(stack[top], "(") == 0) top--;
            else valid = 0;
            i++;
        } else if (isupper(expr[i])) {
            printf("%c ", expr[i]);
            i++;
        }
        // Check AND
        else if (i+2 < len && strncmp(expr+i, "