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,i;
  string s,t;
  cin >> n;
  while(n--){
    cin >> s;
    t = s.substr(0,2);
    s = s.substr(2);
    if(t == ">'"){
      t = s.substr(s.length()-1);
      s = s.substr(0,s.length()-1);
      if(t == "~" && (s.length() % 2) == 1){
	for(i=0;i<s.length();i++){
	  if(i != s.length() / 2 && s[i] != '=' ||
	     i == s.length() / 2 && s[i] != '#') break;
	}
	if(i==s.length()) cout << "A" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else if(t == ">^"){
      t = s.substr(s.length()-2);
      s = s.substr(0,s.length()-2);
      if(t == "~~" && (s.length() % 2) == 0){
	for(i=0;i<s.length();i++){
	  if((i % 2) == 0 && s[i] != 'Q' || (i % 2) == 1 && s[i] != '=') break;
	}
	if(i==s.length()) cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else cout << "NA" << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
  int n,size;
  string s,a,b;
  for(cin>>n;n>0;n--){
    cin>>s;
    size = s.size();
    if(size%2){cout<<"NA"<<endl;continue;}
    a=">'";
    b=">^";
    for(int i=0;i<size/2-2;i++){a=a+"=";b=b+"Q=";}
    a+="#";
    for(int i=0;i<size/2-2;i++){a=a+"=";}
    a+="~";
    b+="~~";
    if (s==a)cout<<"A"<<endl;
    else if (s==b)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);

int main() {
	int n;
	cin >> n;
	REP(i, n) {
		string s;
		cin >> s;
		string h = s.substr(0, 2);
		if(h == ">'") {
			s += "!";
			if(s.find('#') != string::npos) {
				int pos = s.find('#');
				int size = pos-2;
				REP(i, pos-2) {
					if(s[i+2] == s[i+pos+1] && s[i+2] == '=') {

					} else {
						cout << "NA" << endl;
						goto next;
					}
				}
				if(s[pos+size+1] == '~' && s[pos+size+2] == '!') {
					cout << "A" << endl;
					goto next;
				} else {
					cout << "NA" << endl;
					goto next;
				}
				 
			} else {
				cout << "NA" << endl;
				goto next;
			}			
		} else if(h == ">^") {
			s += "!!";
			int b = 2;
			bool flag = false;
			while(s.substr(b, 2) == "Q=") {
				b += 2;
				flag = true;
			}

			if(s.substr(b, 2) == "~~" && s.substr(b+2, 2) == "!!" && flag) {
				cout << "B" << endl;
				goto next;
			} else {
				cout << "NA" << endl;
				goto next;
			}
		} else {
			cout << "NA" << endl;
			goto next;
		}
		next:;
	}

	return 0;
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

vector<string> split(const string &s, const char &delim) {
  istringstream ss(s);
  string got;
  vector<string> v;
  while (getline(ss, got, delim)) v.pb(got);
  return v;
}

void solve() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    if (regex_match(s, regex(">'=+#=+~"))) {
      vs v = split(s, '#');
      if (v[0].size() - 2 == v[1].size() - 1) {
        cout << "A" << endl;
        continue;
      }
    }
    if (regex_match(s, regex(">\\^(Q\\=)+~~"))) {
      cout << "B" << endl;
      continue;
    }
    cout << "NA" << endl;
  }
}

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

using namespace std;

int main()
{
	int n;
	cin >> n;
	for(int D = 0; D < n; ++D){
		string s;
		cin>>s;
		
		if(s.compare(0,2,">'")== 0){
			int bef_eq = 0;
			int aft_eq = 0;
			int pos_shp = 0;
			int pos_tld = 0;

			for(int i = 2; i < s.length(); ++i ){
				switch( s[i] ){
					case '=':
						if( pos_shp == 0 )
							++bef_eq;
						else
							++aft_eq;
						break;
					case '#':
						pos_shp = i;
						break;
					case '~':
						pos_tld = i;
						break;
				}
			}
			if( bef_eq && aft_eq && pos_shp && bef_eq == aft_eq && pos_tld == s.length() - 1 )
				cout << "A\n";
			else
				cout << "NA\n";
		}else if(s.compare(0,2,">^")==0){
			int cnt_qeq = 0;
			bool invalid = false;
			
			if( s.length() <= 2 || s.length() % 2 == 1 )
				invalid = true;
			
			for(int i = 2; i < s.length() && !invalid; i+=2){
				if( s.compare(i,2,"Q=") == 0 )
					++cnt_qeq;
				else if( s.compare(i,2,"~~") == 0 && i == s.length() - 2)
					break;
				else
					invalid = true;
			}
			if( invalid )
				cout << "NA\n";
			else
				cout << "B\n";
		}else{
			cout <<"NA\n";
		}
	}
	return 0;
}