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

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

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;
		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++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < (k+n); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(_Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,_Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; }
}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

string solve(string &s){
    int i = 2;
    if(s[0] == '>' && s[1] == '\''){
        int c = 0;
        while(s[i] == '=') c++, i++;
        if(c == 0 || s[i] != '#') return "NA";
        i++;
        while(s[i] == '=') c--, i++;
        if(c != 0 || s[i] != '~' || s[i+1] != '\0') return "NA";
        return "A";
    }
    else if(s[0] == '>' && s[1] == '^'){
        int c = 0;
        while(s[i] == 'Q' && s[i+1] == '=') c++, i+=2;
        if(c == 0 || s[i] != '~' || s[i+1] != '~' || s[i+2] != '\0') return "NA";
        return "B";
    } else {
        return "NA";
    }
}

int main(){
    int n;
    cin >> n;
    REP(j,n){
        string s;
        cin >> s;
        cout << solve(s) << endl;
    }

    return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
  int n;
  for(cin>>n;n--;)
    {
      string s;
      cin>>s;
      string ans="NA";

	if(s.substr(0,2)==">'" &&s[s.size()-1]=='~')
	  {
	    string tmp=s.substr(2,s.size()-3);
	    //	  cout<<tmp<<endl;
	    if(tmp.size()%2!=0
	       && tmp[tmp.size()/2]=='#'
	       && tmp.substr(0,tmp.size()/2)==tmp.substr(tmp.size()/2+1,tmp.size()/2)
	       && tmp.substr(0,tmp.size()/2)==*(new string(tmp.size()/2,'=')))
		ans="A";
	  }
	else if(s.substr(0,2)==">^" && s.substr(s.size()-2,2)=="~~")
	  {
	    string tmp=s.substr(2,s.size()-4);
	    //	      cout<<tmp<<endl;
	    while(tmp.size()>0)
	      {
		if(tmp.substr(0,2)=="Q=")
		  tmp=tmp.substr(2,tmp.size()-2);
		else
		  break;
	      }
	    if(tmp.size()==0)
	      ans="B";	    
	    }
	  cout<<ans<<endl;
    }
}