#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>
#include <math.h>
#include <string.h>



using namespace std;

bool hebi_a(string hebi){
    if(!(hebi[0]=='>' && hebi[1]=='\''))return false;
    if(hebi[2]!='=')return false;
    
    int now=3;
    int count=1;
    
    while(1){
        if(hebi[now]=='#'){
            now++;
            break;
        }else if(hebi[now]=='='){
            now++;
            count++;
        }else{
            return false;
        }
    }
    
    while(now!=hebi.length()){
        if(hebi[now]=='='){
            now++;
            count--;
        }else if(hebi[now]=='~'){
            now++;
            break;
        }else{
            return false;
        }
    }
    if(now==hebi.length()&& count==0)return true;
    else return false;
}


bool hebi_b(string hebi){
    if(!(hebi[0]=='>' && hebi[1]=='^'))return false;
    if(hebi[2]!='Q')return false;
    if(hebi[3]!='=')return false;
    int now=4;
    while(1){
        if(hebi[now]!='Q'){
            break;
        }else{
            now++;
        }
        
        if(now==hebi.length())return false;
        
        if(hebi[now]!='='){
            return false;
        }else{
            now++;
        }
        
        if(now==hebi.length())return false;
    }
    if(now==hebi.length())return false;
    
    if(hebi[now]!='~')return false;
    now++;
    if(now==hebi.length())return false;
    if(hebi[now]!='~')return false;
    now++;
    if(now==hebi.length())return true;
    else return false;

}


int main(){
    int n;
    cin>>n;
    for(int roop=0; roop<n; roop++){
        string hebi;
        cin>>hebi;
        if(hebi_a(hebi))cout<<"A"<<endl;
        else if(hebi_b(hebi))cout<<"B"<<endl;
        else cout<<"NA"<<endl;
        
    }
}