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 <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


int main() {
	int n;
	string s;
	cin >> n;
	rep(i, n) {
		cin >> s;
		string ans;
		cout << s.size() << endl;
		if (s.size() % 2 == 1||s.size()==4) {
			ans = "NA";
		}
		else if (s[0] == '>'&&s[s.size() - 1] == '~') {
			if (s[1] == '\'') {
				ans = "A";
				for (int i = 2; i < s.size() - 1; i++) {
					if (i == s.size() / 2) {
						if (s[i] != '#') {
							ans = "NA";
							break;
						}
					}
					else {
						if (s[i] != '=') {
							ans = "NA";
							break;
						}
					}
				}
			}
			else if (s[1] == '^'&&s[s.size()-2]=='~') {
				ans = "B";
				for (int i = 2; i < s.size() - 2; i += 2) {
					if (s[i] != 'Q' || s[i + 1] != '=') {
						ans = "NA";
						break;
					}
				}
			}
			else ans = "NA";
		}
		else {
			ans = "NA";
		}
		cout << ans << endl;
	}
}

C++: import java.lang.*;
import java.util.*;

class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		for(int i=0;i<n;i++){
			String s=sc.next();
			if(s.matches("^>'(=+)#\\1~$"))
				System.out.println("A");
			else if(s.matches("^>\\^(Q=)+~~$"))
				System.out.println("B");
			else
				System.out.println("NA");
		}
	}
}

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

bool n[486848]={};
int main(){
	string str,x="  ";
	
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
	cin>>str;
	
	for(int j=0;j<2;j++)
		x[j]=str[j];
	
	if(x==">'"&&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(x==">^"&&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 <iostream>

int main(){
	int n;
	std::cin >> n;
	while(n--){
		std::string sn, res = "";
		std::cin >> sn;

		//Type A
		//ªª>'CKöª~C©Â¨ ÌªªïÅ é
		if(sn.substr(0,2) == ">'" &&
			 sn[sn.size()-1] == '~' && (sn.size()-3) % 2 == 1){
			std::string st = sn.substr(2,sn.size()-3);
			//¨ Ì^ñª#
			if(st[st.size()/2] == '#'){
				st.replace(st.size()/2,1,"");
				
				//#ÈOª·×Ä=
				// int eq = 1;
				// for(int i=0;i<st.size();i++){
				// 	if(st[i] != '='){eq = 0;break;}
				// }
				// if(eq)std::cout << "A" << std::endl;
				if(st != ""){
					int pos;
					while(pos = st.find("="), pos != std::string::npos){
						st.replace(pos,1,"");
					}
					if(st == "")res = "A\n";
				}
			}
		}

		//Type B
		if(sn.substr(0,2) == ">^" &&
			 sn.substr(sn.size()-2,2) == "~~" &&
			 (sn.size()-4) % 2 == 0){
			std::string st = sn.substr(2,sn.size()-4);
			if(st != ""){
				int pos;
				while(pos = st.find("Q="), pos != std::string::npos){
					st.replace(pos,2,"");
				}
				if(st == "")res = "B\n";
			}
		}

		if(res == "")res = "NA\n";
		std::cout << res;
	}
}

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 ] == '\'' )
    {
        S = S.substr( 2, S.size() - 3 );
        if( S.find_first_not_of( "#=", 0 ) != string::npos ) return 0;
        string tmpB = S.substr( 0, S.find('#') );
        string tmpA = S.substr( S.find( '#' ) + 1, S.size() - 1 );
        return tmpB.size() == tmpA.size() ? 1 : 0;
    }
    //B?¨?
    if( S[ 1 ] == '^' && S[ S.size() - 2 ] == '~' )
    {
        S = S.substr( 2, S.size() - 4 );
        if( S.find_first_not_of( "Q=", 0 ) != string::npos ) return 0;
        for( int i = 0; i < S.size(); i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' ) return 0;
        }
        return 2;
    }
    return 0;
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    string S;
    while( cin >> S )
    {
        cout << res[ check( S ) ] << endl;
    }
    return 0;
}