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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int solve(string &s){
  int t = -1;
  if(s.length() < 3) return -1;
  if(s.length() >= 3 && s.substr(0, 2) == ">'" && s[(int)s.length() - 1] == '~') t = 0;
  if(s.length() >= 4 && s.substr(0, 2) == ">^" && s.substr((int)s.length() - 2) == "~~") t = 1;
  if(t == -1) return -1;
  if(t){
    for(int i = 2; i + 3 < s.length(); i += 2) if(s.substr(i, 2) != "Q=") return -1;
    return t;
  } else{
    int c1 = 0, c2 = 0, i = 2;
    for( ; i + 1 < s.length() && s[i] == '='; ++i) ++c1;
    if(s[i++] != '#') return -1;
    for( ; i + 1 < s.length() && s[i] == '='; ++i) ++c2;
    if(c1 != c2 || s[i] != '~' || i + 1 != (int)s.length()) return -1;
    return t;
  }
  return -1;
}

int main() {
  int T; cin >>T;
  REP(t, T){
    string s; cin >>s;
    int ans = solve(s);
    cout <<(ans == 0 ? "A" : (ans == 1 ? "B" : "NA")) <<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, flag2=0, cnt1=0, cnt2=0;
  char snake[10000][200];
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake[i]);
    len = strlen(snake[i]);
    if(snake[i][0] == '>' && snake[i][1] == '\'' /*&& snake[i][2] == '='*/) {
      for(int j = 2 ; j < len - 1 ; j++) {
	if(snake[i][j] == '=' && flag == 0) {cnt1++; flag2 = 1; }
	if(snake[i][j] == '=' && flag == 1) {cnt2++; flag2 = 1; }
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2 && flag2 == 1) cout << 'A' << endl;
      else cout << "NA" << endl;
      flag = 0;
      cnt1 = 0; cnt2 = 0;
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' /*&& snake[i][2] == 'Q' && snake[i][3] == '='*/) {
      for(int j = 2 ; j < len - 3 ; j++) {
	if(snake[i][j] == 'Q' && snake[i][j+1] == '=') flag = 1;
      }
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~'  && flag == 1) cout << 'B' << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
  return 0;
}

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

int main(){
  string str;
  int n; cin >> n;
  while(n--){
    cin >> str;
    int idx = 2;
    int cnt = 0;
    bool flg = true;
    if(str[0] == '>' && str[1] == 39){
      while(str[idx] != '#') {
	cnt++;
	idx++;
      }
      idx++;
      while(str[idx] != '~') {
	cnt--;
	idx++;
      }
      if(cnt == 0) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
      }
      if(str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}

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

using namespace std;

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

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#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>
#include <cassert>
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 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)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
	int n;
	cin>>n;
	REP(t,n){
		string s;
		cin>>s;
		if(s[0]!='>')goto na;
		if(s[1]=='\''){
			int state=0;
			int cnt1=0;
			int cnt2=0;
			FOR(i,2,s.size()){
				if(s[i]=='='&&state==0){
					cnt1++;
				}else if(s[i]=='#'&&state==0){
					state=1;
				}else if(s[i]=='='&&state==1){
					state=2;
					cnt2++;
				}else if(s[i]=='='&&state==2){
					cnt2++;
				}else if(s[i]=='~'&&state==2){
					state=3;
				}else{
					state=-1;
					break;
				}
			}
			if(state==3&&cnt1==cnt2){
				goto a;
			}else{
				goto na;
			}
		}else if(s[1]=='^'){
			int state=0;
			int flag=0;
			FOR(i,2,s.size()){
				if(s[i]=='Q'&&state==0){
					state=1;
				}else if(s[i]=='='&&state==1){
					state=0;
					flag=1;
				}else if(s[i]=='~'&&state==0){
					state=2;
				}else if(s[i]=='~'&&state==2){
					state=3;
				}else{
					state=-1;
					break;
				}
			}
			if(state==3&&flag){
				goto b;
			}else{
				goto na;
			}
		}else{
			goto na;
		}
a:
		cout<<"A"<<endl;
		continue;
b:
		cout<<"B"<<endl;
		continue;
na:
		cout<<"NA"<<endl;
		continue;
	}

}