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>
using namespace std;

int main(){
  
  int n;
  cin >> n;
  while(n--){
    bool judge = false;
    string s; cin >> s;
    if(s[0] == '>'){
      if(s[1] == '\'' && s[s.size()-1] == '~'){
	if(s[s.size() / 2] == '#' && !(s.size() & 1))  cout << "A" << endl;
	else judge = true;
      }
      else if(s[1] == '^' && s[s.size()-1] == s[s.size()-2] && s[s.size()-1] == '~'){
	for(int i = 2; i < s.size()-2; i++){
	  if((!(i & 1) && s[i] != 'Q') || (i & 1 && s[i] != '=')) judge = true;
	}
	if(!judge) cout << "B" << endl;
      }
      else judge = true;
    }
    else judge = true;
    if(judge) cout << "NA" << endl;
  }

}

C++: #include<iostream>
using namespace std;
int main(){

    int n;
    cin>>n;
    while(n--){

        string str;
        cin>>str;
        if(str[0]!='>')cout<<"NA"<<endl;
        else{
            if(str[1]=='\''&&str[2]=='='){
                int e=2;
                for(;str[e]=='=';e++);
                if(str[e]=='#'){
                    int f=e+1;
                    for(;str[f]=='=';f++);
                    if(e==f-e+1&&str[f]=='~'&&str[f+1]=='\0')cout<<"A"<<endl;
                    else cout<<"NA"<<endl;
                }
                else cout<<"NA"<<endl;
            }
            else if(str[1]=='^'&&str[2]=='Q'){
                int e,flg=0;
                for(e=2;str[e]=='Q';e+=2){
                    if(str[e+1]!='='){
                        cout<<"NA"<<endl;
                        flg=1;
                        break;
                    }
                }
                if(!flg){
                    if(str[e]=='~'&&str[e+1]=='~'&&str[e+2]=='\0')cout<<"B"<<endl;
                    else cout<<"NA"<<endl;
                }
            }
            else cout<<"NA"<<endl;
        }

    }

    return 0;
}

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

int main(){
  char buf[210];
  int bi,n,eqc,ti;

  cin >> n;

  for (int i=0;i<n;i++){
    bi=0;
    cin >> buf;
    if (buf[bi++]!='>') {
      cout << "NA" << endl;
      continue;
    }
    if (buf[bi]=='\''){
      bi++;
      eqc=0;
      while (buf[bi+eqc]=='=')eqc++;
      if (buf[bi+eqc]!='#'){
	cout << "NA" << endl;
	continue;
      }
      bi += eqc+1;
      for (ti=0;ti<eqc;ti++)
	if (buf[bi+ti]!='=')break;
      if (ti <eqc){
	cout << "NA" << endl;
	continue;
      }
      bi+=ti;
      if (buf[bi]=='~'&&buf[bi+1]=='\0')cout << "A" << endl;
      else cout << "NA" << endl;
    }else if(buf[bi]=='^'){
      bi++;
      cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      while (buf[bi]=='Q'&&buf[bi+1]=='=')bi +=2;
      cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      if (buf[bi]=='~'&&buf[bi+1]=='~'){
	if (buf[bi+2]=='\0')cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else{
      cout << "NA" << endl;
      continue;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
    int n;
	cin >> n;
	
	while(n--)
	{
		string snk;
		cin >> snk;
		
		if(snk.size() < 2 || snk.size() % 2 != 0 || (snk.substr(0, 2) != ">'" && snk.substr(0, 2) != ">^"))
		{
			cout << "NA" << endl;
		}
		else if(snk.substr(0, 2) == ">^")
		{
			if(6 <= snk.size() && snk.substr(2, 2) == "Q=" && snk.substr(snk.size() - 2, 2) == "~~")
			{
				for(int i = 4; i < snk.size() - 2; i += 2)
				{
					if(snk.substr(i, 2) != "Q=")
					{
						goto ng;
					}
				}
				
				cout << "B" << endl;
			}
			else
			{
			ng:;
				cout << "NA" << endl;
			}				
		}
		else
		{
			if(snk[(snk.size() - 3) / 2 + 2] == '#')
			{
				rep(i, (snk.size() - 3) / 2)
				{
					if(snk[i + 2] != '=' || snk[i + 3 + (snk.size() - 3) / 2] != '=')
					{
						goto ng;
					}
				}
				
				cout << "A" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
	}
}

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

string solv(string s){
	int ss = s.size();
	if(s[0] != '>' || s[ss-1] != '~')return "NA";
	if(s[ss-2] == '~'){
		for(int i = 0;i < ss-2;i++){
			if(s[i] == 'Q')return "B";
		}
	}else{
		int a = 0, f = 0;
		for(int i = 1;i < ss-1;i++){
			if(s[i] == '=' && !f)a++;
			if(s[i] == '#')f = 1;
			if(s[i] == '=' && f)a--;
		}
		return a ? "NA" : "A";
	}
	return "NA";
}

int main(){
	int n;cin >> n;
	string s;
	while(n--){
		cin >> s;
		cout << solv(s) << endl;
	}
	return 0;
}

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

string solve(string T) {
	int chain = 0, ok = 0;
	if (T.size() >= 3) {
		if (T[T.size() - 1] == '~') {
			if (T.substr(0, 2) == ">'" && T.size() >= 6) {
				for (int i = 2; i < T.size() - 1; i++) {
					if (T[i] == '#') {
						ok++;
					}
					else {
						if (ok == 0) { chain++; }
						else { chain--; }
					}
				}
				if (ok == 1 && chain == 0) { return "A"; }
			}

			if (T.substr(0, 2) == ">^" && T[T.size() - 2] == '~' && T.size() >= 6) {
				for (int i = 2; i < T.size() - 2; i += 2) {
					if (T.substr(i, 2) != "Q=") {
						goto E;
					}
				}
				return "B";
			E:;
			}
		}
	}
	return "NA";
}

int main() {
	cin >> n;
	for (int h = 0; h < n; h++) {
		cin >> S;
		cout << solve(S) << endl;
	}
}

C++: #include <string>
#include <iostream>
using namespace std;
int n; string s;
bool check() {
	if(s.size() < 6) return 0;
	if(s[0] != '>') return 0;
	if(s[1] != '\'') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 1; i++) {
			if(s[i] != '=') break;
			cnt++;
		}
		if(s[2 + cnt] != '#') return 0;
		for(int j = cnt + 3; j < s.size(); j++) {
			if(s[i] != '=') break;
			cnt--;
		}
		if(cnt) return 0;
		if(2 + cnt + 1 != s.size()) return 0;
		return s[2 + cnt] == '~' ? 1 : 0;
	}
	if(s[1] != '^') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 2; i += 2) {
			if(s[i] != 'Q' || s[i + 1] != '=') break;
			cnt += 2;
		}
		if(cnt + 4 != s.size()) return 0;
		return (s[cnt + 2] == '~' && s[cnt + 3] == '~') ? 2 : 0;
	}
	return 0;
}
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		cin >> s;
		printf("%s\n", !check() ? "NA" : (check() == 1 ? "A" : "B"));
	}
	return 0;
}