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<stdio.h>
#include<string.h>

int main(void)
{
	char str[]="特に意味のない文章";
	int cut_i,cut_j;
	int len,kaisu;
	int hantei;

	scanf("%d",&kaisu);

	for(cut_i=0;cut_i<kaisu;cut_i++){
		scanf("%s",str);
		len = strlen(str);
		hantei = 0;

		if((len < 5 )|| (str[0] != '>') || (len % 2 != 0)){
			printf("NA\n");
			continue;
		}
		
		len--;		

		if((str[1] == '\'') && (str[len] == '~') ){
			cut_j = 2;
			while(str[cut_j] == '='){
				hantei++;
				cut_j++;
			}

			if(str[cut_j] != '#'){
				printf("NA\n");
				continue;
			}

			cut_j++;

			while(str[cut_j] == '='){
				hantei--;
				cut_j++;
			}

			if( hantei == 0){
				printf("A\n");
			}
			else {
				printf("NA\n");
			}
		}

		else if((str[1] == '^') &&(str[len-1] == str[len]) && (str[len] == '~')){
			for(cut_j =2;cut_j<len-2;cut_j++){
				if((str[cut_j] != 'Q') || (str[cut_j+1] != '=')){
					hantei = -1;
				}
				cut_j++;
			}
			printf("B\n");
		}
		else {
			printf("NA\n");
		}			
	}	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main(){
	int n;
	cin>>n;
	rep(i,0,n){
		string s;
		cin>>s;
		int ans=0;
		int sz=s.size();
		if(s[0]=='>'&&s[1]=='\''&&s[sz-1]=='~'&&sz%2==0&&s[sz/2]=='#'){
			rep(i,2,sz-1){
				if(i!=sz/2&&s[i]!='='){
					ans=-1; break;
				}
				if(i==sz-2) ans=1;
			}
		}else if(s[0]=='>'&&s[1]=='^'&&s[sz-2]=='~'&&s[sz-1]=='~'&&sz%2==0){
			rep(i,2,sz-2){
				if(i%2==0&&s[i]!='Q'){
					ans=-1; break;
				}if(i%2&&s[i]!='='){
					ans=-1; break;
				}
				if(i==sz-3) ans=2;
			}
		}else{
			ans=-1;
		}
		if(ans==-1) o("NA");
		else if(ans==1) o("A");
		else if(ans==2) o("B");
	}	
}

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] == '#') {
      break;
    } else {
      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;
    } 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 true;
    } 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() == 2) {
      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 <iostream>
#include <string>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
int main()
{
    int n; cin >> n;
    while (n--) {
        string s; cin >> s;
        string head = s.substr(0, 2);
        bool ok = true;
        if (head == ">'") {
            int c = 0;
            REP(i, 2, s.size()) { if (s[i] != '=') break; c++; }
            if (c < 1) ok = false;
            else if (s[c+2] != '#') ok = false;
            else if (s.substr(c+3, c) != string(c, '=')) ok = false;
            else if (s.size() != c*2 + 4) ok = false;
            else if (s[s.size()-1] != '~') ok = false;
            puts(ok ? "A" : "NA");
        }
        else if (head == ">^") {
            if (s.substr(2, 2) != "Q=") ok = false;
            else {
                for (int i = 4; i < s.size()-2; i+=2)
                    if (s.substr(i, 2) != "Q=") { ok = false; break; }
                if (ok && s.substr(s.size()-2) != "~~") ok = false;
            }
            puts(ok ? "B" : "NA");
        }
        else puts("NA");
    }
    return 0;
}

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

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

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

string solv(string s){
	int ss = s.size();
	if(s[0] != '>' || s[ss-1] != '~')return "NA";
	if(s[ss-2] == '~'){
		if(s[1] != '^')return "NA";
		for(int i = 0;i < ss-2;i++){
			if(s[i] == 'Q' && s[i+1] == '=')return "B";
		}
	}else{
		if(s[1] != '\'')return "NA";
		int a = 0, f = 0;
		for(int i = 1;i < ss-1;i++){
			if(s[i] == '=' && !f)a++;
			if(s[i] == '#')f = 1;
			if(f && !a)return "NA";
			if(s[i] == '=' && f)a--;
		}
		return a ? "NA" : "A";
	}
	return "NA";
}

int main(){
	int n;cin >> n;
	string s;
	while(n--){
		cin >> s;
		cout << solv(s) << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int f1=0,f2=0,k=-1;
    bool f;
    for(int i=0;i<s.size();i++)if(s[i]=='>')k=i;
    if(k==-1){
      printf("NA\n");
      continue;
    }
    if(s[k+1]=='^'){
      f=false;
      for(int i=k;i<s.size()-1;i++)if(s[i]=='Q'&&s[i+1]=='=')f=true;
      if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
        printf("B\n");
        continue;
      }
    }
    int cnt=0;
    for(int i=k;i<s.size();i++){
      if(s[i]=='='){
        while(s[i]=='='){
          cnt++;i++;
        }
        k=i;
        break;
      }
    }
    f=false;
    for(int i=k;i<s.size();i++){
      if(s[i]=='#'){
        k=i;f=true;
      }
    }
    if(f){
      for(int i=k;i<s.size();i++){
        if(s[i]=='='){
          while(s[i]=='='){
            cnt--;i++;
          }
          break;
        }
      }
      if(cnt==0&&s[s.size()-1]=='~'){
        printf("A\n");
        continue;
      }
    }
    printf("NA\n");
  }
}