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 q=0;q<n;q++) {
    string str;
    cin >> str;
    if(str[0] != '>') {
      cout <<"NA"<<endl; 
      continue;
    }  else if(str[1] != '\''&& str[1] != '^') {
      cout << "NA" << endl;
      continue;
    }
    int flg = 0,sum;
    if(str[1] == '\'') {
      int t = 0;
      sum=0;
      for(int i=2;i<str.size();i++) {
	if(str[i] == '=') t++;
	else if(str[i] == '#' && sum==0 && i!=2) sum = t , t = 0;
	else break;
	if(str[str.size()-1] == '~' && i == str.size() -2 && sum==t ) flg = 1;
	
      }
    }

      if(str[1] == '^' && flg == 0) {
	flg = 0;
	for(int i=2;i<str.size() ;i++) {
	  if(i%2 == 0 && str[i] != 'Q') break;
	  if(i%2 == 1 && str[i] !='=')  break;

	  if(str[str.size()-2] == '~' && str[str.size()-1] =='~' && i == str.size()-3&& i%2==1 ){
	    flg = 2;
	    break;
	  }
	}
      }

      if(flg == 0) cout << "NA" << endl;
      else if(flg == 1)cout <<"A" << endl;
      else if(flg == 2) cout << "B"<<endl;
  }     


  return 0;
}

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

using namespace std;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string a;
		cin >> a;
		if (a.substr(0, 2) == ">'") {
			int counter = 0;
			bool hantei = false;
			int i;
			for (i = 2; i < a.size(); i++) {
				if (a[i] == '=') {
					counter++;
				} else if (a[i] == '#' && counter > 0) {
					hantei = true;
					break;
				} else {
					hantei = false;
					break;
				}
			}
			if (hantei) {
				int j;
				for (j = i+1; j < a.size(); j++) {
					if (a[j] == '=') {
						counter--;
					} else if (a[j] == '~') {
						hantei = true;
						break;
					} else {
						hantei = false;
						break;
					}
				}
				if (hantei) {
					if (counter == 0 && j == a.size()-1) {
						cout << 'A' << endl;
					} else {
						cout << "NA" << endl;
					}
				} else {
					cout << "NA" << endl;
				}
			} else {
				cout << "NA" << endl;
			}
		} else if (a.substr(0, 2) == ">^") {
			int i;
			bool hantei = false;
			for (i = 2; i < a.size()-1; i += 2) {
				if (a.substr(i, 2) == "Q=") {
					hantei = true;
				} else if (a.substr(i, 2) == "~~" && i == a.size()-2 && hantei) {
					hantei = true;
					break;
				} else {
					hantei = false;
					break;
				}
			}
			if (hantei) {
				cout << 'B' << endl;
			} else {
				cout << "NA" << endl;
			}
		}else {
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <string>
#include <iostream>
using namespace std;
int n; string s;
int check() {
	if(s.size() < 6) return 0;
	if(s[0] != '>') return 0;
	if(s[1] == '\'') {
		int cnt = 0, cntp = 0;
		for(int i = 2; i < s.size() - 1; i++) {
			if(s[i] != '=') break;
			cnt++; cntp++;
		}
		if(s[2 + cnt] != '#') return 0;
		for(int i = cnt + 3; i < s.size(); i++) {
			if(s[i] != '=') break;
			cnt--; cntp++;
		}
		if(cnt) return 0;
		if(2 + cntp + 2 != s.size()) return 0;
		return (s[2 + cntp + 1] == '~' ? 1 : 0);
	}
	if(s[1] == '^') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 2; i += 2) {
			if(s[i] != 'Q' || s[i + 1] != '=') break;
			cnt += 2;
		}
		if(cnt + 4 != s.size()) return 0;
		return ((s[cnt + 2] == '~' && s[cnt + 3] == '~') ? 2 : 0);
	}
	return 0;
}
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		cin >> s;
		cout << s << ' ' << check() << endl;
	}
	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;
int check( string S )
{
    //A
    if( S[ 0 ] == '>' && S[ 1 ] == '\'' && S[ S.size() - 1 ] == '~' && S.size() >= 6 )
    {
        S = S.substr( 2, S.size() - 3 );
        if( S.find_first_not_of( "#=", 0 ) != string::npos ) return 0;
        string tmpB = S.substr( 0, S.find('#') );
        string tmpA = S.substr( S.find( '#' ) + 1, S.size() - 1 );
        return tmpB.size() == tmpA.size() ? 1 : 0;
    }
    //B
    if( S[ 0 ] == '>' && S[ 1 ] == '^' && S[ S.size() - 1 ] == '~' && S[ S.size() - 2 ] == '~' && S.size() >= 6 )
    {
        S = S.substr( 2, S.size() - 4 );
        if( S.find_first_not_of( "Q=", 0 ) != string::npos ) return 0;
        if( S.size() % 2 != 0 ) return 0;
        for( int i = 0; i < S.size(); i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' ) return 0;
        }
        return 2;
    }
    return 0;
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    string S;
    while( cin >> S )
    {
        cout << res[ check( S ) ] << endl;
    }
    return 0;
}

C++: #include <stdio.h>

bool checkA(char *p){
    int eqCnt=0;
    
    if (*p++!='>') return false;
    if (*p++!='\'') return false;
    while (*p++=='=') eqCnt++;
    p--;
    if (*p++!='#') return false;
    while (*p++=='=') eqCnt--;
    p--;
    if (eqCnt!=0) return false;
    if (*p++!='~') return false;
    if (*p!='\0') return false;
    
    return true;
}

bool checkB(char *p){
    if (*p++!='>') return false;
    if (*p++!='^') return false;
    while (*p++=='Q'){
        if (*p++!='=') return false;
    }
    p--;
    if (*p++!='~' || *p++!='~') return false;
    if (*p!='\0') return false;
    
    return true;
}

int main(void){
    int n;
    char str[256];
    
    scanf("%d", &n);
    for (int i=0; i<n; i++){
        scanf("%s", str);
        if (checkA(str)){
            puts("A");
        } else if (checkB(str)){
            puts("B");
        } else {
            puts("NA");
        }
    }
    
    return 0;
}

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

using namespace std;

bool kind_a(string str){
	
	if(str[0] != '>' || str[1] != '\'' || str[str.size()-1] != '~'){
		return false;
	}
	
	string su = str.substr(2, str.size()-3);
	
	for(int i=0; i<su.size(); ++i){
		
		int j = su.size()-1-i;
		
		if(i>=j)
			break;
			
		if(su[i]!='=' || su[i]!=su[j]){
			return false;
		}	
	}
	
	return su[(su.size()-1)/2]=='#';
}

bool kind_b(string str){
		
	if(str[0] != '>'  || str[1] != '^' || 
		str[str.size()-2] != '~' || str[str.size()-1] != '~'){
		return false;
	}
	
	string su = str.substr(2, str.size()-4);
	
	for(int i=0; i<su.size(); ++i){
		
		int j = su.size()-1-i;
		
		if(i>=j)
			break;
			
		if( !(su[i]=='Q' && su[j]=='=' || su[j]=='Q' && su[i]=='=') ){
			return false;
		}
	}
	
	return true;
}

string solve(string str){
	
	if(kind_a(str)){
		return "A";
	}
	else if(kind_b(str)){
		return "B";
	}
	else{
		return "NA";
	}
}


int main(){

	int n;
	cin>>n;
	
	while(n--){
		
		string str;
		cin>>str;
		cout << solve(str) << endl;
	}
	
	return 0;
}