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 <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 flag1=0, flag2=0, flag3=0;
  int true_flag=0, true_flag2=0;
  int cnt1=0, cnt2=0;
  
  for(int i = 0 ; i < len - 1 ; i++) {
    if(snake[i] != '>' && snake[i] != '#' && snake[i] != '=' && snake[i] != '\'') { true_flag2 = 1; break; }
    if(snake[i] == '>' && snake[i+1] == '\'') flag2 = 1;
    if(snake[i] == '=' && flag1 == 0 && flag2 == 1) {cnt1++; flag3 = 1; }
    if(snake[i] == '=' && flag1 == 1 && flag2 == 1) {cnt2++; flag3 = 1; }
    if(snake[i] == '#' && flag2 == 1 && flag3 == 1) flag1 = 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] != '^' && snake[i] != '=' && snake[i] != 'Q') { true_flag2 = 1; break; }
      if(snake[i] == '>' && snake[i+1] == '^') flag3 = 1;
      if(snake[i] == 'Q' && snake[i+1] == '=' && flag3 == 1) flag1 = 1;
    }
    if(snake[len-2] == '~' && snake[len-1] == '~'  && flag1 == 1 && flag3 == 1) {
      return 2;
      true_flag = 1;
    }
  }

  if(true_flag == 0 && true_flag2 == 1) {
    return 3;
  }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define INF 999999
using namespace std;
int main(){
    int N,j;
    cin>>N;
    while(N--){
        int runcnt=0,j;
        char S[150]={0};
        cin>>S;
            if(S[0]=='>'&&S[1]==39){
                int K=0;
                for(j=2;;j++){
                    if(S[j]=='='){
                        runcnt++;
                        K=1;
                    }
                    else if(S[j]=='#'){
                        break;
                    }
                    else {
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
                for(int i=j+1;;i++){
                    if(S[i]=='='){
                        runcnt--;
                    }
                    else if(runcnt==0&&S[i]=='~'&&K==1){
                        cout<<"A"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else if(S[0]=='>'&&S[1]=='^'){
                int F=0;
                for(j=2;;){
                    if(S[j]=='Q'&&S[j+1]=='='){
                        j+=2;
                        F=1;
                    }
                    else if(S[j]=='~'&&S[j+1]=='~'&&F==1){
                        cout<<"B"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else cout<<"NA"<<endl;
            end:;
    }
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;


int main(){
	string str;
	
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
	cin>>str;
		string x="  ";
	
if(str[0]=='>'){
	for(int j=0;j<2;j++)
		x[j]=str[j];
	
	if(str[1]=='\''&&str[str.size()-1]=='~'){
		bool f=true;int C=0;
		int mid=(2+str.size()-2)/2;
		for(int j=2;j<=str.size()-2;j++)
		{if(j!=mid&&str[j]!='='){f=false;}
		else if(j==mid&&str[j]!='#')f=false;
		if(str[j]=='=')C++;
		}
		if(C%2!=0||C==0)f=false;
		
		if(f==true)cout<<"A"<<endl;
		else cout<<"NA"<<endl;
	}

	else if(str[1]=='^'&&str[str.size()-1]=='~'&&str[str.size()-2]=='~'){
		bool f=true;int C=0;
		
		for(int j=2;j<str.size()-2;j++)
		{
			C++;
			if(C%2==0&&str[j]!='=')f=false;
			if(C%2==1&&str[j]!='Q')f=false;

		}
	
		if(C%2!=0||C==0)f=false;
		
		if(f==true)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	
	
	}
	else cout<<"NA"<<endl;

	}
	else cout<<"NA"<<endl;


	}
	return 0;
}

C++: #include<stdio.h>
int main(){
  char s[201]={0};
  int i,n,j,c,h=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    h=0;
    scanf("%s",s);
    if(s[0]=='>'){
      if(s[1]=='\''){
	c=0;
	for(j=2;s[j]=='=';j++)c++;
	if(s[j++]=='#'){
	  for(;s[j]=='=';j++)c--;
	  if(c==0&&s[j]=='~'&&s[j+1]==0)h=1;
	}
      }
      if(s[1]=='^'){
	h=2;
	for(j=2;s[j]=='Q';j+=2){
	  if(s[j+1]!='='){
	    h=0;
	    break;
	  }
	}
	if(!(s[j]=='~'&&s[j+1]=='~'&&s[j+2]==0))h=0;
      }
    }
    if(h==0)printf("NA\n");
    if(h==1)printf("A\n");
    if(h==2)printf("B\n");
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  cin >> n;
  while(n--) {
    string s;
    cin >> s;
    int f=0;
    if (s.substr(0,2) == ">'") {
      int i=2, j1=0,j2=0;
      for (;i<s.length() && s[i]=='='; i++) j1++;
      if (i<s.length() && s[i++] == '#') {
        for (;i<s.length() && s[i]=='='; i++) j2++;
        if (j1>0 && j1==j2 && i<s.length() && s[i] == '~') {
          cout << "A";
          f++;
        }  
      }
    } else if (s.substr(0,2) == ">^") {
      int i = 2;
      for (;i<s.length()-1 && s.substr(i,2) == "Q="; i+=2) ;
      if (i>2 && s.substr(i,2) == "~~") {
        cout << "B";
        f++;
      }
    }
    if (f==0)
      cout << "NA";
    cout << endl;
  }
}