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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

bool isa(string s){
    if(s.substr(0,2) != ">'") return false;
    int x = 2;
    int c = 0;
    while(s[x+c] == '='){
	c++;
    }
    if(s[x+c] != '#') return false;
    x = x + c + 1;
    for(int i=0;i<c;i++){
	if(s[x+i] != '=') return false;
    }
    x += c;
    if(s[x] != '~') return false;
    return true;
}

bool isb(string s){
    if(s[0] != '>') return false;
    if(s[1] != '^') return false;
    int x = 2;
    int c = 0;
    while(s[x+c] == 'Q' && s[x+1+c] == '='){
	c += 2;
    }
    x += c;
    if(s[x] != '~') return false;
    if(s[x+1] != '~') return false;
    return true;
}

int main(){
    string s = "";
    int n;
    cin >> n;
    REP(i,n){
	cin >> s;
	if(isa(s)) cout << "A"<<endl;
	else if(isb(s)) cout << "B" << endl;
	else cout << "NA" << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main(){
	int n;
	while(cin>>n,n){
		for(int x=0;x<n;++x){
			string line;
			cin>>line;
			string sol = "NA";
			if(line.find('#')!=string::npos){
				bool flag = false;
				string a = "";
				string b = "";
				for(int x=2;x<line.size()-1;++x){
					if(line[x] == '#'){
						flag = true;
						continue;
					}
					if(!flag){
						a += line[x];
					}else{
						b += line[x];
					}
				}
				if(line.size()> 3 && line[0]=='>' && line[1]=='\'' && line[line.size()-1]=='~' && a==b)sol = "A";
			}else{
				bool flag = true;
				for(int x=2;line.size()>4 && x<(line.size()-3);x+=2){
				
					//cout<<"OK"<<endl;
					if(line[x] !='Q' || line[x+1] != '=')flag = false;
				}
				if(line.size()>4 && line[0]=='>' && line[1]=='^' && line[line.size()-1]=='~' && line[line.size()-2]=='~' && flag)sol = "B";
			}
			cout<<sol<<endl;
		}
	}
	return 0;
}

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

string S ;

int funcA(){
	int cnt = 0 , i ;
	for( i=2 ; i<S.size() ; i++ ){
		if( S[i] == '=' ) cnt++ ;
		else if( S[i] == '#' ) break ;
		else return -1 ;
	}
	if( cnt == 0 ) return -1 ;
	for( i++ ; i<S.size() ; i++ ){
		if( S[i] == '=' ) cnt-- ;
		else if( S[i] == '~' ) break ;
		else return -1 ;
	}
	if( cnt == 0 ) return 1 ;
	return -1 ;
}

int funcB(){
	int cnt = 0 ;
	for( int i=2 ; i<S.size() ; i+=2 ){
		if( S[i] == 'Q' && S[i+1] == '=' ) cnt++ ;
		else if( S[i] == '~' && S[i+1] == '~' && cnt ) return 2 ;
		else return -1 ;
	}
	return -1 ;
}

int main(){
	
	int N ;
	
	cin >> N ;
	while( N-- ){
		cin >> S ;
		int ans = -1 ;
		if( S.size() < 6 || S.size()%2 == 1 ){}
		else if( S[0] == '>' && S[1] == '\'' ){ ans = funcA() ; }
		else if( S[0] == '>' && S[1] == '^' ){ ans = funcB() ; }
		
		if( ans == -1 ) cout << "NA" << endl ;
		if( ans == 1 ) cout << "A" << endl ;
		if( ans == 2 ) cout << "B" << endl ;
	}
	
return 0 ;
}

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

int main(void){
  string str;
  int n;
  cin>>n;
  while(n--){
    cin>>str;
    int ca = 0;
    int cntsha = 0;
    bool a = true;
    if(str.size()%2 == 0){
      if(str[str.size() / 2] == '#')str[str.size()/2] = '=';
      for(int i = 2 ; i < str.size() - 1 ; i ++){
	if(str[i] != '='){
	  a = false;
	  break;
	}
      } 
      if(a)ca = 1;
    }
    if(ca != 1){
    bool b = true;
    for(int i = 2 ; i < str.size() - 2 ; i ++){
      //cout<<str[i];
      if(i%2){
	if(str[i] != '='){
	  b = false;
	  //cout<<i<<endl;
	  break;
	}
      }else{
	if(str[i] != 'Q'){
	  b = false;
	  //cout<<i<<endl;
	  break;
	}	
      }
    }
    if(b){
      ca = 2;
    }
    }
    if(ca == 1){
      cout<<"A"<<endl;
    }else if(ca == 2){
      cout<<"B"<<endl;
    }else{
      cout<<"NA"<<endl;
    }
  }
}