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; cin >> n;
  for(int i = 0; i < n; i++) {
    string s; cin >> s;
    bool a[4] = {}, b[3] = {};
    int cnt = 0;
    for(int j = 0; j < s.size(); j++) {
      if(s[j] == '>') {
        if(!a[1] && s[j+1] == '\'') a[0] = true;
        if(s[j+1] == '^') b[0] = true;
      } else if(a[0] && s[j] == '=') {
        a[1] = true;
        cnt++;
      } else if(b[0] && s.substr(j,2) == "Q=") {
        b[1] = true;
      } else if(a[1] && s[j] == '#') {
        a[0] = false;
        a[2] = true;
      } else if(a[2] && s[j] == '=') {
        cnt--;
      } else if(a[1] && cnt == 0 && j+1 == s.size() && s[j] == '~') {
        a[3] = true;
      } else if(b[1] && j+2 == s.size() && s.substr(j,2) == "~~") {
        b[2] = true;
      }
    }

    if(a[3]) cout << 'A' << endl;
    else if(b[2]) 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]=='\''&&s.size()%2==0){
      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]!='='){cout<<i;break;}
      }
    }
    else if(s[0]=='>'&&s[1]=='^'&&s[2]=='Q'){
        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 <iostream>
#include <string>
using namespace std;

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

C++: #include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  getchar();
  for(int i = 0; i < n; i++) {
    string snake;
    getline(cin, snake);

    string ans = "NA";
    if(snake[0] == '>' && snake[1] == '\'' && snake.length() >= 6) {
      int shape = 0;
      for(int i = 2; i < snake.length(); i++) {
	if(i+1 >= snake.length()) {
	  if(snake[i] == '~') ans = "A";
	} else {
	  if(snake[i] != '=' && snake[i] != '#') break;
	  if(snake[i] == '#') {
	    if(shape == 1) break;
	    else {
	      shape++;
	      if(i-2 != (double)(snake.length()-4)/2) break;
	    }
	  }
	}
      }
    } else if(snake[0] == '>' && snake[1] == '^' && snake.length()%2 == 0 && snake.length() >= 6) {
      for(int i = 3; i < snake.length(); i+=2) {
	if(i+2 >= snake.length()) {
	  if(snake[i-1] == '~' && snake[i] == '~') ans = "B";
	} else {
	  if(snake[i-1] != 'Q' || snake[i] != '=') break;
	}
      }
    }

    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
  
  int Tc; cin >> Tc;
  
  while(Tc--) {
    string S; cin >> S;
    
  LA:
    {
      string s = S;
      
      if(s.size() < 6) goto BAD;
      if(s.substr(0, 3) != ">'=") goto LB;
      if(s[s.size()-1] != '~') goto BAD;
      
      s = s.substr(2);
      s = s.substr(0,s.size()-1);
      
      int pos = -1;
      int const size = s.size();
      int cnt = 0;
      for(int i=0; i<size; i++) {
	if(s[i] == '=') cnt++;
	else if(s[i] == '#') { pos = i; break; }
	else goto BAD;
      }
      if(pos == -1) goto BAD;
      if(2*cnt+1 != s.size()) goto BAD;
      
      for(int i=cnt+1; i<2*cnt+1; i++) {
	if(s[i] != '=') goto BAD;
      }
      
      cout << "A" << endl; goto EXIT;
    }
    
  LB:
    {
      string s = S;
      
      if(s.substr(0, 2) != ">^") goto BAD;
      if(s.substr(s.size()-2) != "~~") goto BAD;
      
      s = s.substr(2);
      s = s.substr(0, s.size()-2);
      
      if(s.size() % 2 || s.empty()) goto BAD;
      
      int const size = s.size();
      for(int i=0; i<size; i+=2) {
	if(!(s[i] == 'Q' && s[i+1] == '=')) goto BAD;
      }
      
      cout << "B" << endl; goto EXIT;
    }
    
  BAD:;
    cout << "NA" << endl;
    
  EXIT:;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++) 
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string &S )
{
    auto sz = S.size();
    //?????????[>'=], ????°????[~]?????????, size()??? 6 ??\????????????????????? true
    if( S.find( ">'=" ) == 0 && S.find( "~" ) == sz - 1 && sz >= 6 )
    {
        //??????[>']??¨????°?[~]???????????????
        S = S.substr( 2, sz - 3 );
        sz = S.size();
        //??°?????? S ???[#,=]??\???????????????????????????????????? false
        if( S.find_first_not_of( "#=", 0 ) != string::npos )
        {
            return 0;
        }
        //??°?????? S ????????? ????????????????????£???[#]???????????\??????[#]????????£?????? false
        auto pos = S.find( "#" );
        if( S.find( "#", pos + 1 ) != string::npos )
        {
            return 0;
        }
        // S ???????????????[#]???????????£??????, ????????? S ?????¶??°?????£?????? false
        if( S[ sz / 2 ] != '#' || sz % 2 == 0 )
        {
            return 0;
        }
        return 1;
    }
    return 0;
}
bool check_B( string &S )
{
    auto sz = S.size();
    if( S.find( ">^Q=" ) == 0 && S.find( "~~" ) == sz - 2 )
    {
        S = S.substr( 2, sz - 4 );
        sz = S.size();
        if( sz % 2 != 0 )
        {
            return 0;
        }
        for( int i = 0; i < sz; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' )
            {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    REP( i, N )
    {
        string S;
        cin >> S;
        cout << res[ check_A( S ) ? 1 : check_B( S ) ? 2 : 0 ] << endl;
    }
    return 0;
}