#include <stdio.h>

int main(void) {
    int quantity, i, j, idx, count = 0;
    char snake[200] = {0};
    
    scanf("%d", &quantity);
    
    for(i = 0; i < quantity; i++) {
        scanf("%s", snake);
        if(snake[0] != '>') {
            puts("NA");
            break;
        } else if(snake[1] == '\'') {
            
            for(j = 2; j < 199; j++) {
                if(snake[j] == '=') {
                    count++;
                } else if(snake[j] == '#') {
                    idx = j;
                    break;
                }
            }
            
            for(j = idx + 1; j < 199; j++) {
                if(snake[j] != '=') {
                    idx = j;
                    break;
                }
                count--;
            }
            
            if(count == 0) {
                if(snake[idx] == '~')
                    puts("A");
                else
                    puts("NA");
            } else {
                puts("NA");
            }
            
        } else if(snake[1] == '^') {
            
            for(j = 2; j < 198; j += 2) {
                if(snake[j] == 'Q')
                    count++;
                if(snake[j + 1] == '=')
                    count--;
                if(snake[j] == '~' && snake[j + 1] == '~')
                    break;
                printf("count = %d\n", count);
                
            }
            
            if(count == 0)
                puts("B");
            else
                puts("NA");
            
        }
    }
    return(0);
}