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++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#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()

int main() {
  int n;
  cin >> n;
  while(n--) {
    string s;
    cin >> s;
    int f=0;
    if (s.substr(0,2) == ">'") {
      int i=2, j1=0,j2=0;
      for (;i<s.length() && s[i]=='='; i++) j1++;
      if (i<s.length() && s[i++] == '#') {
        for (;i<s.length() && s[i]=='='; i++) j2++;
        if (j1==j2 && i<s.length() && s[i] == '~') {
          cout << "A";
          f++;
        }  
      }
    } else if (s.substr(0,2) == ">^") {
      int i = 2;
      for (;i<s.length()-1 && s.substr(i,2) == "Q="; i+=2) ;
      if (s.substr(i,2) == "~~") {
        cout << "B";
        f++;
      }
    }
    if (f==0)
      cout << "NA";
    cout << endl;
  }
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<double,int> P;

static const double eps = 10e-9;

bool isA(string str){
	if(str.size() >= 1 && str[0] != '>') return false;
	if(str.size() >= 2 && str[1] != '\'') return false;

	int hist[] = {0,0};
	for(int i=2,j=0;i<str.size()-1;i++){
		if(str[i] == '='){
			hist[j]++;
		}
		else if(str[i] == '#') j++;
		else return false;
	}

	if(hist[0] != hist[1]) return false;
	if(hist[0] == 0 || hist[1] == 0) return false;
	if(str[str.size()-1] != '~') return false;

	return true;

}

bool isB(string str){
	if(str.size() >= 1 && str[0] != '>') return false;
	if(str.size() >= 2 && str[1] != '^') return false;

	if(str.size() <= 3) return false;

	string rear = str.substr(2,str.size()-2);

	bool isok=false;
	int i;
	for(i=0;i+1<rear.size();){
		if(rear.substr(i,2) == "Q="){
			isok=true;
			i+=2;
		}
		else break;
	}

	return (i+2 == rear.size() && isok && rear.substr(i,2) == "~~");
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			string str;
			cin >> str;

			if(isA(str)) cout << "A" << endl;
			else if(isB(str)) cout << "B" << endl;
			else cout << "NA" << endl;
		}
	}
	
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  int i,n;
  string s;
  cin>>n;
  while(n--){
    cin>>s;
    if(s[0]=='>'&&s[1]=='\''){
      for(i=2;i<s.size();i++){
	if(i==s.size()/2&&s[i]!='#')break;
	if(i==s.size()/2){i++;continue;}
	if(i==s.size()-1&&s[i]=='~'){
	  cout<<"A"<<endl;
	  goto L;
	}
	if(s[i]!='=')break;
      }
    }
    else if(s[0]=='>'&&s[1]=='^'){
        for(i=2;i<s.size();i++){
	if(i==s.size()-2&&s[i]=='~'&&s[i+1]=='~'){
	  cout<<"B"<<endl;
	  goto L;
	}
	if(s[i]!='Q'||s[i+1]!='=')break;
	else i++;
      }
    }
    cout<<"NA"<<endl;
  L:;
  }
}

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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

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

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;

int Judge(string &s){
	if(s[0] != '>') return -1;
	if(s.size() < 2) return -1;
	if(s[1] == '\''){
		if(s[s.size()-1] != '~') return -1;
		int len = sz(s) - 3;
		if(len < 3 || len % 2 == 0) return -1;
		FOR(i,(len-1)/2){
			if(s[2+i] != '=' || s[2+len/2+1+i] != '=') return -1;
		}
		if(s[2+len/2] != '#') return -1;
		return 0;
	} else if(s[1] == '^'){
		if(s[s.size()-1] != '~' || s[s.size()-2] != '~')return -1;
		int len = sz(s) - 4;
		if(len < 2 || len % 2 != 0) return -1;
		FOR(i,len/2){
			if(s[2+2*i] != 'Q' || s[2+2*i+1] != '=') return -1;
		}
			return 1;
	} else {
		return -1;
	}
}

int main(){
	int n; cin>>n;
	string out[] = {"NA","A","B"};
	FOR(i,n){
		string s; cin>>s;
		int a = Judge(s);
		cout << out[a+1] << endl;
	}
	return 0;
}