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 D = 0; D < n; ++D){
		string s;
		cin>>s;
		
		if(s.compare(0,2,">'")== 0){
			int bef_eq = 0;
			int aft_eq = 0;
			int shp = 0;
			int pos_shp = 0;
			int pos_tld = 0;

			for(unsigned int i = 2; i < s.length(); ++i ){
				switch( s[i] ){
					case '=':
						if( shp == 0 )
							++bef_eq;
						else
							++aft_eq;
						break;
					case '#':
						shp++;
						break;
					case '~':
						if( i != s.length() - 1 )
							shp = 2;
						pos_tld = i;
						break;
					default:
						shp = 2;
						break;
				}
			}
			if( bef_eq && aft_eq && pos_tld && bef_eq == aft_eq && shp == 1 )
				cout << "A\n";
			else
				cout << "NA\n";
		}else if(s.compare(0,2,">^")==0){
			int cnt_qeq = 0;
			bool invalid = false;
			
			if( s.length() <= 2 || s.length() % 2 == 1 )
				invalid = true;
			
			for(unsigned int i = 2; i < s.length() && !invalid; i+=2){
				if( s.compare(i,2,"Q=") == 0 )
					++cnt_qeq;
				else if( s.compare(i,2,"~~") == 0 && i == s.length() - 2)
					break;
				else
					invalid = true;
			}
			if( invalid )
				cout << "NA\n";
			else
				cout << "B\n";
		}else{
			cout <<"NA\n";
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

int main(){

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

bool isA(string s) {
  int cnt1 = 0;
  for (int i = 2; i < s.size(); ++i) {
    if (s[i] == '=') {
      ++cnt1;
    } else if (s[i] == '#') {
      if (cnt1 == 0) return false;
      break;
    } else {
      return false;
    }
    if (i == s.size() - 1) return false;
  }
  int cnt2 = 0;
  for (int i = cnt1 + 3; i < s.size(); ++i) {
    if (s[i] == '=') {
      ++cnt2;
    } else if (s[i] == '~') {
      return (cnt1 == cnt2 && i == s.size() - 1);
    } else {
      return false;
    }
  }
  return false;
}

bool isB(string s) {
  if (s.size() % 2) return false;
  for (int i = 2; i < s.size(); i += 2) {
    if (s[i] == 'Q' && s[i + 1] == '=') {
      continue;
    } else if (s[i] == '~' && s[i + 1] == '~') {
      return i + 1 == s.size() - 1;
    } else {
      return false;
    }
  }
  return false;
}

int main() {
  int n; cin >> n;
  vector<string> s(n); for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 0; i < n; ++i) {
    if (s[i][0] != '>' || s[i].size() == 3) {
      cout << "NA" << endl;
      continue;
    }
    if (s[i][1] == '\'') {
      cout << ( (isA(s[i])) ? "A" : "NA") << endl;
    } else if (s[i][1] == '^') {
      cout << ( (isB(s[i])) ? "B" : "NA") << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

bool isOutOfStr(int ind, string str){
  return ind >= str.length();
}

int main(void){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    if(!isOutOfStr(1, str) &&str[0] == '>' && str[1] == '\''){
      int sharp_cnt = 0;
      int ind;
      ind = 2;

      if(!isOutOfStr(ind, str) && str[ind] != '='){
        cout << "NA" << endl;
        continue;
      }

      for(;;ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          break;
        }
        sharp_cnt++;
      }

      if(isOutOfStr(ind, str) || str[ind] != '#'){
        cout << "NA" << endl;
        continue;
      }
      ind++;

      bool flg = true;
      for(int i=0; i < sharp_cnt; i++, ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          cout << "NA" << endl;
          flg = false;
          break;
        }
      }
      if(!flg) continue;
      
      if(!isOutOfStr(ind, str) && str[ind] == '~' && ind == str.length() - 1)
        cout << "A" << endl;
      else{
        cout << "NA" << endl;
      }



    }else if(str[0] == '>' && str[1] == '^'){
      int ind;
      ind=2;

      int flg = true;
      for(;;ind+=2){
        if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
          break;
        }
      }

      if(!isOutOfStr(ind, str) && !isOutOfStr(ind+1, str) && str[ind] == '~' && str[ind+1] == '~' && ind+1 == str.length() - 1){
        cout << "B" << endl;
      }else{
        cout << "NA" << endl;
      }
    }else{
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, cnt1=0, cnt2=0;
  char snake[10000][200];
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake[i]);
    len = strlen(snake[i]);
    if(snake[i][0] == '>' && snake[i][1] == '\'' && snake[i][2] == '=') {
      for(int j = 3 ; j < len - 2 ; j++) {
	if(snake[i][j] == '=' && flag == 0) cnt1++;
	if(snake[i][j] == '=' && flag == 1) cnt2++;
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2) cout << 'A' << endl;
      else cout << "NA" << endl;
      flag = 0;
      cnt1 = 0; cnt2 = 0;
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' && snake[i][2] == 'Q' && snake[i][3] == '=') {
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~') cout << 'B' << endl;
    }
    else cout << "NA" << endl;
  }
  return 0;
}