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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int n;
  while(n--){
    string snake;
    cin>>snake;
    if(snake[0]=='>'&&snake[1]=='\''&&snake[snake.size()-1]=='~'){
      int count=0;
      int end;
      for(int i=2;i<snake.size()&&snake[i]=='='; i++){
        count++;
        end = i;
      }
      end++;
      if(count&&snake[end]=='#'&&end+count+2==snake.size()){
        bool yes = true;
        for(int i=end+1;i<snake.size()-1;i++){
          if(snake[i]!='=')yes = false;
        }
        if(yes){
          cout<<"A"<<endl;
        }else{
          cout<<"NA"<<endl;
        }
      }else{
        cout<<"NA"<<endl;
      }
    }else if(snake[0]=='>'&&snake[1]=='^'&&snake[snake.size()-1]=='~'&&snake[snake.size()-2]=='~'){
      bool yes = true;
      for(int i=2;i<snake.size()-2;i++){
        if(i%2==0&&snake[i]!='Q') yes = false;
        if(i%2==1&&snake[i]!='=') yes = false;
      }
      if(yes){
        cout<<"B"<<endl;
      }else{
        cout<<"NA"<<endl;
      }
    }
  }
  return 0;
}

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

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main() {

  int n;
  int flg=0;
  cin>> n;
  while(n--) {
    string s;
    int cnt=0;
    flg=0;
    cin>> s;
    if(s[0] == '>') {
      if(s[1] == '\'') {
	int equal[2]={0}; // 0:左 1:右
	int now=0; 
	for(int i=2; i<s.size(); i++) {
	  if(s[i] == '=') {
	    equal[now]++;
	  }
	  else if(s[i] == '#') {
	    now=1;
	  }
	}
	if(equal[0] == equal[1]) {
	  flg=1;
	}
      }

      else if(s[1] == '^') {
	for(int i=2; i<s.size()-2; i+=2) {
	  if(s[i] != 'Q' && s[i] != '=') {
	    break;
	  }
	  cnt=i;
	}
	if(cnt == (s.size()-4) && s[s.size()-2] == '~' &&  s[s.size()-1] == '~') {
	  flg=2;
	}
      }
    }

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

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

int main(){
  string str;
  int n; cin >> n;
  while(n--){
    cin >> str;
    int idx = 2;
    int cnt = 0;
    bool flg = true;
    if(str[0] == '>' && str[1] == 39){
      bool used = false;
      while(idx < str.size() && str[idx] == '=') {
	used = true;
	cnt++;
	idx++;
      }
      if(str[idx] == '#'){
	idx++;
	while(idx < str.size() && str[idx] == '=') {
	  cnt--;
	  idx++;
	}
      }
      if(cnt == 0 && used) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(idx+1 < str.size() && str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
	cnt++;
      }
      if(cnt > 0 && str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}

C++: #include<iostream>
#include<string>
using namespace std;
string s;
int main(){
	int n,t,now;
	cin>>n;
	while(n--){
		cin>>s;
		if(s.size()>=6&&s.substr(0,3)==">'="){
			t=0;
			while(s[3+(t++)]=='=');
			if(s[3+t-1]!='#')
				cout<<"NA"<<endl;
			else
			if(4+2*t!=s.size()||s.substr(3+t,t)!=s.substr(2,t)){

				cout<<"NA"<<endl;
			}
			else
			if(s[3+2*t]=='~')
				cout<<"A"<<endl;
		}
		else if(s.size()>=6&&s.substr(0,4)==">^Q="){
			now=4;
			int pre=0;
			while(1){
				if(s.size()-now<2){
					pre=1;
					break;
				}
				if(s.substr(now,2)!="Q=")
					break;
				now+=2;
			}
			if(pre==1)
			cout<<"NA"<<endl;
			else
			if(s.size()-now==2&&s.substr(now,2)=="~~"){
				cout<<"B"<<endl;
			}
			else {cout<<"NA"<<endl;

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

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
using namespace std;
int main(){
  int n;
  cin>>n;
  rep(i,n){
    string ans;
    string s;
	cin>>s;
	if(s[0]!='>'){cout<<"NA"<<endl;continue;}
	if(s[1]=='^'){
	  ans='B';
	  int h=2;
	  if(s[h]=='Q'&&s[h+1]=='=')while(s[h++]=='Q'&&s[h++]=='=');
	  h--;
      if(s[h++]!='~'||s[h++]!='~')ans="NA";
	  if(h!=s.size())ans="NA";
	}else if(s[1]=='\''){
	  ans='A';
	  int h1=0,h2=0;
	  int j=1;
	  while(s[++j]=='=')h1++;
	  if(s[j]!='#')ans="NA";
	  while(s[++j]=='=')h2++;
	  if(s[j]!='~')ans="NA";
	  if(++j!=s.size())ans="NA";
	  if(h1!=h2)ans="NA";
	}
	cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


int main() {
	int n;
	string s;
	cin >> n;
	rep(i, n) {
		cin >> s;
		string ans;
		cout << s.size() << endl;
		if (s.size() % 2 == 1||s.size()==4) {
			ans = "NA";
		}
		else if (s[0] == '>'&&s[s.size() - 1] == '~') {
			if (s[1] == '\'') {
				ans = "A";
				for (int i = 2; i < s.size() - 1; i++) {
					if (i == s.size() / 2) {
						if (s[i] != '#') {
							ans = "NA";
							break;
						}
					}
					else {
						if (s[i] != '=') {
							ans = "NA";
							break;
						}
					}
				}
			}
			else if (s[1] == '^'&&s[s.size()-2]=='~') {
				ans = "B";
				for (int i = 2; i < s.size() - 2; i += 2) {
					if (s[i] != 'Q' || s[i + 1] != '=') {
						ans = "NA";
						break;
					}
				}
			}
			else ans = "NA";
		}
		else {
			ans = "NA";
		}
		cout << ans << endl;
	}
}