Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		ans := "NA"
		scanner.Scan()
		s := scanner.Text()
		if s[:2]==">'" && s[len(s)-1:] == "~" {
			tmp := s[2:len(s)-1]
			t := strings.Split(tmp, "#")
			if len(t) == 2 && len(t[0]) > 0 {
				valid := true
				for _, v := range t[0] {
					if string(v) != "=" {
						valid = false
						break
					}
				}
				if valid && t[0] == t[1] {
					ans = "A"
				}
			}
		} else if s[:2] == ">^" && s[len(s)-2:] == "~~" {
			t := s[2:len(s)-2]
			if len(t) > 0 && len(t)%2 == 0 {
				valid := true
				for i := 0; i < len(t); i += 2 {
					if t[i:i+2] != "Q=" {
						valid = false
						break
					}
				}
				if valid {
					ans = "B"
				}
			}
 		}
		fmt.Println(ans)
	}
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;


int main(){
	string str;
	
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
	cin>>str;
		string x="  ";
	

	for(int j=0;j<2;j++)
		x[j]=str[j];
	if(str[0]=='>'){
	if(str[1]=='\''&&str[str.size()-1]=='~'){
		bool f=true;int C=0;
		int mid=(2+str.size()-2)/2;
		for(int j=2;j<=str.size()-2;j++)
		{if(j!=mid&&str[j]!='='){f=false;}
		else if(j==mid&&str[j]!='#')f=false;
		if(str[j]=='=')C++;
		}
		if(C%2!=0)f=false;
		
		if(f==true)cout<<"A"<<endl;
		else cout<<"NA"<<endl;
	}

	else if(str[1]=='^'&&str[str.size()-1]=='~'&&str[str.size()-2]=='~'){
		bool f=true;int C=0;
		
		for(int j=2;j<str.size()-2;j++)
		{
			C++;
			if(C%2==0&&str[j]!='=')f=false;
			if(C%2==1&&str[j]!='Q')f=false;

		}
	
		if(C%2!=0)f=false;
		
		if(f==true)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	
	
	}
	}
	else cout<<"NA"<<endl;


	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++) 
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int check( string S )
{
    //???????????¢>??£, ????°??????¢~??£ ????????????????????????NA
    if( S[ 0 ] != '>' || S[ S.size() - 1 ] != '~' )
    {
        return 0;
    }
    //A?¨?
    if( S[ 1 ] == '\'' )
    {
        if( S.substr( 2, S.size() - 3 ).find_first_not_of( "#=", 0 ) != string::npos ) return 0;
        string tmpB = S.substr( 2, S.find('#') - 2 );
        string tmpA = S.substr( S.find('#') + 1, S.size() - ( S.find('#') + 2 ) );
        return tmpB.size() == tmpA.size() ? 1 : 0;
    }
    //B?¨?
    if( S[ 1 ] == '^' && S[ S.size() - 2 ] == '~' )
    {
        if( S.substr( 2, S.size() - 4 ).find_first_not_of( "Q=", 0 ) != string::npos ) return 0;
        for( int i = 2; i < S.size() - 2; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + i ] != '=' ) return 0;
        }
        return 2;
    }
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    string S;
    while( cin >> S )
    {
        cout << res[ check( S ) ] << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;


int main(){

	int n,length,second_length;
	char snake[201];

	scanf("%d",&n);

	for(int i=0; i < n; i++){
		scanf("%s",snake);

		if(snake[1] == '\0'){
			printf("NA\n");
			continue;
		}

		if(snake[0] == '>' && snake[1] == '\''){
			if(snake[2] != '='){
				printf("NA\n");
			}else{
				for(length = 0;snake[length+2] != '\0' && snake[length+2] == '='; length++);

				if(snake[length+2] != '#'){
					printf("NA\n");
				}else{
					for(second_length = 0; snake[length+3+second_length] != '\0' &&
								snake[length+3+second_length] == '=';second_length++);

					if(length != second_length || snake[length+3+second_length] != '~'){
						printf("NA\n");
					}else{
						if(snake[length+4+second_length] == '\0'){
							printf("A\n");
						}else{
							printf("NA\n");
						}
					}
				}
			}
		}else if(snake[0] == '>' && snake[1] == '^'){
			if(snake[2] != 'Q' || snake[3] != '='){
				printf("NA\n");
			}else{
				bool breakFLG = false;
				for(length=0;snake[length+2] != '\0' && (snake[length+2] == 'Q' || snake[length+2] == '=');length++){
					if(length > 1){
						if(snake[length+2] == 'Q' && snake[length+2-1] != '='){
							breakFLG = true;
							break;
						}
						if(snake[length+2] == '=' && snake[length+2-1] != 'Q'){
							breakFLG = true;
							break;
						}
					}
				}
				if(breakFLG)printf("NA\n");
				else{
					if(snake[length+2-1] == 'Q'|| snake[length+2] != '~'){
						printf("NA\n");
					}else{
						if(snake[length+3] != '~'){
							printf("NA\n");
						}else{
							if(snake[length+4] == '\0'){
								printf("B\n");
							}else{
								printf("NA\n");
							}
						}
					}
				}
			}
		}else{
			printf("NA\n");
		}


	}

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	string s;
	int n;
	cin>>n;
	rep(i,n){
		cin>>s;
		int size=s.size();
		string A="";
		string B="";
		rep(i,size){
			if(i==0){A+=">";B+=">";}
			else if(i==1){A+="'";B+="^";}
			else{
				if(i<size-1){
					if(i==(size-3)/2+2)A+="#";
					else A+="=";
				}else A+="~";
				if(i<size-2){
					if(i%2==0)B+="Q";else B+="=";
				}else{B+="~";}
			}
		}
		if(size%2==0){
			if(s==A)cout<<"A"<<endl;
			else if(s==B)cout<<"B"<<endl;
			else cout<<"NA"<<endl;
		}else cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;

int main(){
	int n,a,c,f;
	string str = "\0";
	cin >> n;
	for(int i = 0;i < n;++i){
		cin >> str;
		c = f = 0;
		a = 2;
		if(str[0] == '>'){
			if(str[1] == '\''){
				while(str[a] == '='){
					++a;
					++c;
				}
				if(str[a] != '#' || c == 0) goto end;
				++a;
				while(str[a] == '='){
					++a;
					++f;
				}
				if(str[a] == '~' && c == f && c != 0) cout << "A" << endl;
				else goto end;
			}
			else if(str[1] == '^'){
				while(str[a] == 'Q' && str[a+1] == '='){
					a += 2;
				}
				if(str[a] == '~' && str[a+1] == '~') cout << "B" << endl;
				else goto end;
			}
			else goto end;
		}
		else {
			end:
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int solve(string &s){
  int t = -1;
  if(s.length() < 3) return -1;
  if(s.length() >= 3 && s.substr(0, 2) == ">'" && s[(int)s.length() - 1] == '~') t = 0;
  if(s.length() >= 4 && s.substr(0, 2) == ">^" && s.substr((int)s.length() - 2) == "~~") t = 1;
  if(t == -1) return -1;
  if(t){
    for(int i = 2; i + 2 < s.length(); i += 2) if(s.substr(i, 2) != "Q=") return -1;
    return t;
  } else{
    int c1 = 0, c2 = 0, i = 2;
    for( ; i + 1 < s.length() && s[i] == '='; ++i) ++c1;
    if(s[i++] != '#') return -1;
    for( ; i + 1 < s.length() && s[i] == '='; ++i) ++c2;
    if(c1 != c2 || s[i] != '~' || i + 1 != (int)s.length()) return -1;
    return t;
  }
  return -1;
}

int main() {
  int T; cin >>T;
  REP(t, T){
    string s; cin >>s;
    int ans = solve(s);
    cout <<(ans == 0 ? "A" : (ans == 1 ? "B" : "NA")) <<endl;
  }
  return 0;
}