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;

bool isA(const string& s){
	if( s.size() >= 1 && s[0] != '>' ) return false;
	if( s.size() >= 2 && s[1] != '\'' ) return false;
	
	int cnt1 = 0, cnt2 = 0;
	for(int i=2, j=0 ; i < s.size()-1 ; i++ ){
		if( s[i] == '=' ){
			j? cnt1++ : cnt2++;
		}else if( s[i] == '#' ){
			j++;
		}else{
			return false;
		}
	}
	if( cnt1 == 0 || cnt2 == 0 || cnt1 != cnt2 || s[s.size()-1] != '~' ){
		return false;
	}
	return true;
}

bool isB(const string& s){
	if( s.size() >= 1 && s[0] != '>' ) return false;
	if( s.size() >= 1 && s[1] != '^' ) return false;
	if( s.size() <= 6 || s.size() % 2 == 1 ) return false;
	
	bool flag = false;
	for(int i=3 ; i < s.size() - 2 ; i += 2 ){
		if( s[i-1] != 'Q' || s[i] != '=' ){
			return false;
		}
	}
	
	if( s[s.size()-2] != '~' || s[s.size()-1] != '~' ){
		return false;
	}
	return true;
}

int main(){
	int n;
	cin >> n;
	for( ; n-- ; ){
		string s;
		cin >> s;
		
		if( isA( s ) ){
			cout << "A" << endl;
		}else if( isB( s ) ){
			cout << "B" << endl;
		}else{
			cout << "NA" << endl;
		}
	}
}

C++: //Name: Snakes
//Level: 2
//Category: 文字列,オートマトン
//Note: 

/**
 * AとBは別々に判定する。
 */
#include <iostream>
#include <string>

using namespace std;

bool snakeA(const string &s) {
    auto it = begin(s);
    int cnt = 0;
    if(*it++ != '>') return false;
    if(*it++ != '\'') return false;
    while(true) {
        const char c = *it++;
        if(c == '#') break;
        else if(c == '=') ++cnt;
        else return false;
    }
    while(cnt--) {
        if(*it++ != '=') return false;
    }
    if(*it++ != '~') return false;
    return it == end(s);
}

bool snakeB(const string &s) {
    auto it = begin(s);
    if(*it++ != '>') return false;
    if(*it++ != '^') return false;
    while(true) {
        const char c = *it++;
        if(c == '~') break;
        else if(c != 'Q') return false;

        if(*it++ != '=') return false;
    }
    if(*it++ != '~') return false;
    return it == end(s);
}

bool solve(bool first) {
    string s;
    cin >> s;
    if(snakeA(s)) cout << "A" << endl;
    else if(snakeB(s)) cout << "B" << endl;
    else cout << "NA" << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(2);

    bool first = true;
    int N;
    cin >> N;
    while(N-- && solve(first)) {
        first = false;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	char buf[256];
	int n;
	scanf("%d\n", &n);
	while (n--)
	{
		cin.getline(buf, sizeof(buf));
		string s(buf);
		bool ok = false;
		for (int i = 1; i < 100; ++i)
		{
			string a = ">'";
			for (int j = 0; j < i; ++j)
				a += "=";
			a += "#";
			for (int j = 0; j < i; ++j)
				a += "=";
			a += "~";
			if (s == a)
			{
				puts("A");
				ok = true;
				break;
			}

			string b = ">^";
			for (int j = 0; j < i; ++j)
				b += "Q=";
			b += "~~";
			if (s == b)
			{
				puts("B");
				ok = true;
				break;
			}
		}
		if (!ok)
			puts("NA");
	}

	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+1] == '!') {
					cout << "A" << endl;
					goto next;
				} else {
					cout << "NA" << endl;
					goto next;
				}
				 
			} else {
				cout << "NA" << endl;
				goto next;
			}			
		} else if(h == ">^") {
			s += "!!";
			int b = 2;
			while(s.substr(b, 2) == "Q=") {
				b += 2;
			}
			if(s.substr(b, 2) == "~~" && s.substr(b+2, 2) == "!!") {
				cout << "B" << endl;
				goto next;
			} else {
				cout << "NA" << endl;
				goto next;
			}
		} else {
			cout << "NA" << endl;
			goto next;
		}
		next:;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<complex>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;

bool isA(const string &str){
  int n = str.size();
  int m = 0;
  int pos = 2;
  if(n == 0 || n==1) return false;
  if(str[0] != '>') return false;
  if(str[1] != '\'') return false;
  while(pos < n && str[pos] == '=') {m++; pos++;}
  if(pos == n) return false;
  if(str[pos++] != '#') return false;
  while(pos < n && str[pos] == '=') {m--; pos++;}
  if(pos == n) return false;
  if(str[pos++] != '~') return false;
  if(m == 0 && pos == n) return true;
  return false;
}

bool isB(const string &str){
  int n = str.size();
  int pos = 2;
  int m = 0;
  if(n == 0 || n == 1) return false;
  if(str[0] != '>') return false;
  if(str[1] != '^') return false;
  while(pos+1 < n && str[pos] == 'Q' && str[pos+1] == '=') {m++; pos+=2;}
  if(m == 0) return false;
  if(pos+2 != n) return false;
  if(str[pos] == '~' && str[pos+1] == '~') return true;
  return false;
}

int main(){
  int n;
  string str;
  getline(cin,str); sscanf(str.c_str(),"%d",&n);
  while(n --> 0){
    getline(cin,str);
    if(isA(str)) puts("A");
    else if(isB(str)) puts("B");
    else puts("NA");
  }
  return 0;
}