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(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;
    }
  }
}
#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;
    }
  }
}

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;
      for(int i=2;i<str.size();i++) {
	if(str[i] == '=') t++;
	else if(str[i] == '#') sum = t , t = 0;
	else if(str[i] == '~' && i == str.size() -1 && sum==t) flg = 1;
	else break;
      }
    }

      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 ){
	    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(void){

  string s;
  int n;
  cin >> n;

  while(n--){

    cin >> s;
    if( s[1] == '\'' ){
      int cnt1 = 0;
      int i;
      for( i = 2 ; s[i] == '=' ; i++ ){
	cnt1++;
      }
      if( s[i] != '#' ){
	cout << "NA" << endl;
	goto skip;
      }
      else i++;
      int cnt2 = 0;
      for( ; s[i] == '=' ; i++ ){
	cnt2++;
      }
      if( cnt1 == cnt2 && s[i] == '~' )
	cout << "A" << endl;
      else cout << "NA" << endl;
    }
    else if( s[1] == '^' ){
      if( s[2] == 'Q' && s[3] == '=' && s[s.size() - 1] == '~' && s[s.size() - 2] == '~' ){
	cout << "B" << endl;
      }
    }
    else cout << "NA" << endl;
  skip:;
  }
  return 0;
}

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

int main(){
	int n;	cin >> n;
	while(n--){
		int i,eq1,eq2;
		string ret = "NA";
		
		string s; cin >> s;
		if(s.substr(0,3) == ">'="){
			i = 2;
			
			eq1 = 0;
			while(s[i] == '=' && i < s.length() )i++,eq1++;
			if(s[i] != '#')goto label;
			
			i++; eq2 = 0;
			while(s[i] == '=' && i < s.length() )i++,eq2++;
			
			if(i == s.length()-1 && s[i] == '~' && eq1 == eq2)
				ret = "A"; 
		}else if(s.substr(0,4) == ">^Q="){
			for(i=2;i+1<s.length()-2;i+=2){
				if(s[i] == 'Q' && s[i+1] == '='){}
				else goto label;
			}
			if(s[s.length()-1] == '~' && s[s.length()-2] == '~' && s[s.length()-3] == '=' && s[s.length()-4] == 'Q')
				ret = "B";
			
		}
		label:;
		cout << ret << endl;
		
		
	}
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int checksnake(string, int);

int main(void) {
  int n, len;
  char snake[200];
  int type;
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake);
    len = strlen(snake);
    type = checksnake(snake, len);
    if(type == 1) cout << 'A' << endl;
    else if(type == 2) cout << 'B' << endl;
    else if(type == 3) cout << "NA" << endl;
    for(int a = 0 ; a < 200 ; a++) {
      snake[a] = 0;
    }
    len = 0;
  }
  return 0;
}

int checksnake(string snake, int len) {
  int flag=0, flag2=0, flag3=0;
  int true_flag=0;
  int cnt1=0, cnt2=0;
  
  for(int i = 0 ; i < len - 1 ; i++) {
    if(snake[i] == '>' && snake[i+1] == '\'') flag2 = 1;
    if(snake[i] == '=' && flag == 0 && flag2 == 1) {cnt1++; flag3 = 1; }
    if(snake[i] == '=' && flag == 1 && flag2 == 1) {cnt2++; flag3 = 1; }
    if(snake[i] == '#' && flag2 == 1 && flag3 == 1) flag = 1;
    //cout << cnt1 << ' ' << cnt2 << endl;
  }
  if(snake[len-1] == '~' && cnt1 == cnt2 && flag2 == 1 && flag3 == 1) {
    return 1;
    true_flag = 1;
  }
  
  if(true_flag == 0) {
    for(int i = 0 ; i < len - 2 ; i++) {
      if(snake[i] == '>' && snake[i+1] == '^') flag3 = 1;
      if(snake[i] == 'Q' && snake[i+1] == '=' && flag3 == 1) flag = 1;
    }
    if(snake[len-2] == '~' && snake[len-1] == '~'  && flag == 1 && flag3 == 1) {
      return 2;
      true_flag = 1;
    }
  }

  if(true_flag == 0) {
    return 3;
  }
}