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 <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;
    }
    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;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());
int main(){
	int n;
	cin>>n;
	rep(i,n){
		string s;
		cin>>s;
		if(s.substr(0,2)==">'"&&s.substr(s.size()-1,1)=="~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-1);
			bool f=true;
			while(s.size()>1){
				if(s[0]==s[s.size()-1]&&s[0]=='='){
					s=s.substr(1,s.size()-2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				if(s[0]=='#'){
					cout<<"A\n";
				}
				else cout<<"NA\n";
			}
			else{
				cout<<"NA\n";
			}
		}
		else if(s.substr(0,2)==">^"&&s.substr(s.size()-2,2)=="~~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-2);
			bool f=true;
			while(s.size()>=2){
				if(s.substr(0,2)=="Q="){
					s=s.substr(2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				cout<<"B\n";
			}
			else cout<<"NA\n";
		}
		else{
			cout<<"NA\n";
		}
	}
}

C++: 
#include<iostream>
#include<string>
using namespace std;
main(){
  int f,i,m;
  string s;
  for(cin>>f;cin>>s;f?puts(f-2?"A":"B"):puts("NA")){
    f=0;
    if(s[0]+s[1]==101){
      if((m=s.find(35))!=string::npos)
	for(i=1;s[m-i]==s[m+i];)
	  if(m-++i==1 && m+i==s.size()-1 && s[m+i]=='~')f=1;
    }else if(s[0]+s[1]==156){
      for(i=2;s[i]=='Q'&&s[i+1]=='=';)
	if(++++i==s.size()-2 && s[i]+s[i+1]==252)f=2;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

bool isA(string s) {
  int cnt1 = 0;
  for (int i = 2; i < s.size(); ++i) {
    if (s[i] == '=') {
      ++cnt1;
    } else if (s[i] == '#') {
      break;
    } else {
      return false;
    }
  }
  int cnt2 = 0;
  for (int i = cnt1 + 3; i < s.size(); ++i) {
    if (s[i] == '=') {
      ++cnt2;
    } else if (s[i] == '~') {
      return (cnt1 == cnt2 && i == s.size() - 1);
    } else {
      return false;
    }
  }
  return false;
}

bool isB(string s) {
  if (s.size() % 2) return false;
  for (int i = 2; i < s.size(); i += 2) {
    if (s[i] == 'Q' && s[i + 1] == '=') {
      continue;
    } else if (s[i] == '~' && s[i + 1] == '~') {
      return i + 1 == s.size() - 1;
    } else {
      return false;
    }
  }
  return false;
}

int main() {
  int n; cin >> n;
  vector<string> s(n); for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 0; i < n; ++i) {
    if (s[i][0] != '>' || s[i].size() == 2) {
      cout << "NA" << endl;
      continue;
    }
    if (s[i][1] == '\'') {
      cout << ( (isA(s[i])) ? "A" : "NA") << endl;
    } else if (s[i][1] == '^') {
      cout << ( (isB(s[i])) ? "B" : "NA") << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    bool f=s[0]=='>';
    int l=s.size();
    f&=l>5;
    if(s[1]=='\''){
      int x=0;
      int i;
      for(i=2;i<l;i++){
	if(s[i]=='=') x++;
	else{
	  f&=s[i]=='#';
	  break;
	}
      }
      for(i++;i<l;i++){
	if(s[i]=='=') x--;
	else{
	  f&=s[i]=='~';
	  break;
	}
      }
      f&=i==l-1&&x==0;
    }else if(s[1]=='^'){
      int i;
      for(i=2;i<l;i+=2){
	if(!(i+1<l)){
	  f=0;
	  break;
	}
	if(s[i]=='Q'&&s[i+1]=='=') continue;
	if(i+2==l&&s[i]=='~'&&s[i+1]=='~') continue;
	break;
      }
      f&=i==l;
    }else f=0;
    cout<<(f?(s[1]=='^'?"B":"A"):"NA")<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string snake;
		cin >> snake;
		if(snake.size() < 4)
		{
			cout << "NA" << endl;
		}
		else if(snake[0] == '>' && snake[1] == '\'' && snake[snake.size() - 1] == '~')
		{
			bool first = true;
			int count1 = 0;
			int count2 = 0;
			for(int i = 2; i < snake.size() - 1; ++i)
			{
				if(snake[i] == '=')
				{
					if(first)
					{
						++count1;
					}
					else
					{
						++count2;
					}
				}
				else if(snake[i] == '#')
				{
					first = false;
				}
			}
			if(count1 == count2)
			{
				cout << "A" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else if(snake[0] == '>' && snake[1] == '^' && snake[snake.size() - 2] == '~' && snake[snake.size() - 1] == '~')
		{
			bool flag = true;
			for(int i = 2; i < snake.size() - 2; i += 2)
			{
				if(snake[i] != 'Q' || snake[i + 1] != '=')
				{
					flag = false;
					break;
				}
			}
			if(flag)
			{
				cout << "B" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}