#include <iostream>
#include <string>
using namespace std;

bool isSnakeA(string snake) {
    if (snake[0] != '>' || snake[1] != '\'' || snake[snake.length() - 1] != '~') {
        return false;
    }
    int pre = 0, post = 0;
    bool sharpFlag = false;
    for (int i = 2; i < (int) snake.length() - 1; i++) {
        if (!sharpFlag) {
            switch (snake[i]) {
            case '=':
                pre++;
                break;
            case '#':
                sharpFlag = true;
                break;
            default:
                return false;
                break;
            }
        } else {
            if (snake[i] == '=') {
                post++;
            } else {
                return false;
            }
        }
    }
    return pre > 0 && pre == post;
}

bool isSnakeB(string snake) {
    if (snake[0] != '>' || snake[1] != '^' || snake[snake.length() - 2] != '~' || snake[snake.length() - 2] != '~') {
        return false;
    }
    int count = 0;
    bool QFlag = false;
    for (int i = 2; i < (int) snake.length() - 2; i++) {
        switch (snake[i]) {
        case 'Q':
            if (QFlag) {
                return false;
            } else {
                QFlag = true;
            }
            break;
        case '=':
            if (QFlag) {
                count++;
                QFlag = false;
            } else {
                return false;
            }
            break;
        default:
            return false;
            break;
        }
    }
    return count > 0;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string snake;
        cin >> snake;
        if (isSnakeA(snake)) {
            cout << "A" << endl;
        } else if (isSnakeB(snake)) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}