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

bool isSnakeA(string snake) {
    if (snake.substr(0, 2) != ">'" || snake[snake.length() - 1] != '~') {
        return false;
    }
    snake.erase(snake.begin(), snake.begin() + 2);
    snake.erase(snake.end() - 1, snake.end());
    size_t pos = snake.find('#');
    if (pos == string::npos || snake.substr(0, pos) != snake.substr(pos + 1, snake.length() - pos)) {
        return false;
    }
    return true;
}

bool isSnakeB(string snake) {
    if (snake.substr(0, 2) != ">^" || snake.substr(snake.length() - 2, 2) != "~~") {
        return false;
    }
    snake.erase(snake.begin(), snake.begin() + 2);
    snake.erase(snake.end() - 2, snake.end());
    if (snake.length() % 2 != 0) {
        return false;
    }
    while (snake.empty() == false) {
        if (snake.substr(0, 2) != "Q=") {
            return false;
        }
        snake.erase(snake.begin(), snake.begin() + 2);
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        string snake;
        cin >> snake;
        if (isSnakeA(snake)) {
            cout << "A" << endl;
        } else if (isSnakeB(snake)) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}