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

enum { A, B, NA };
int f(string s){
    if(s.size() < 6) return NA;
    if(s.substr(0,2)==">'"){
        size_t i=2;
        while(i<s.size() && s[i]=='=') i++;
        if(i==2 || i==s.size() || s[i]!='#') return NA;
        size_t j=i+1;
        while(j<s.size() && s[j]=='=') j++;
        if(i-2 != j-i-1 || j==s.size() || s[j]!='~') return NA;
        return (j+1==s.size() && s[j]=='~') ? A : NA;
    } else if(s.substr(0,2)==">^"){
        size_t i=2;
        while(i<s.size() && s[i]=="Q="[i&1]) i++;
        if((i&1)==1 || i==2 || i==s.size() || s[i]!='~') return NA;
        return (i+2==s.size() && s.substr(i)=="~~") ? B : NA;
    } else {
        return NA;
    }
}

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        string s;
        cin >> s;
        char res[3][3] = {"A","B","NA"};
        puts(res[f(s)]);
    }
}