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 <iostream>
#include <string>
#include <algorithm>
#define int long long
#define rep(i, n) for (int i=0; i<n; i++)
#define REP(n) rep(i, n)
using namespace std;

int n;
string snake, head, tail, body;

signed main()
{
    cin >> n;

    REP(n)
    {   
        cin >> snake;

        if (snake.length() < 6)
        {   
            cout << "NA" << endl;
            continue;
        }   

        head = snake.substr(0, 2); 
        if (head == ">'")
        {   
            tail = snake.substr(snake.length() - 1, 1); 
            if (tail != "~")
            {   
                cout << "NA" << endl;
                continue;
            }   
            body = string((snake.length() - 4) / 2, '=') + "#" + string((snake.length() - 4) / 2, '=');
            if (snake.substr(2, snake.length() - 3) == body)
            {   
                cout << "A" << endl;
                continue;
            } else {
                cout << "NA" << endl;
                continue;
            }   
        } else if (head == ">^") {
            tail = snake.substr(snake.length() - 2, 2); 
            if (tail != "~~")
            {   
                cout << "NA" << endl;
                continue;
            }   
            body = ""; 
            int cnt = (snake.length() - 4) / 2;
            while (cnt--) body += "Q=";
            if (snake.substr(2, snake.length() - 4) == body)
            {
                cout << "B" << endl;
                continue;
            } else {
                cout << "NA" << endl;
                continue;
            }
        } else {
            cout << "NA" << endl;
            continue;
        }
    }

    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
bool A(string s){
  if(s[0] != '>' ||
     s[1] != '\'' ||
     s[s.size() - 1] != '~')
    return false;
  if(s.size()%2==1)return false;
  if(s[s.size()/2] != '#')return false;
  s[s.size()/2] = '=';
  loop(i,2,s.size() - 1){
    if(s[i] != '=')return false;
  }
  //cout<<s<<endl;
  return true;
}
bool B(string s){
  if(s[0] != '>' ||
     s[1] != '^' ||
     s[s.size() - 2] != '~' ||
     s[s.size() - 1] != '~' )
    return false;
  loop(i,2,s.size()-2){
    if(i%2==0){
      if(s[i] != 'Q')return false;
    }else{
      if(s[i] != '=')return false;
      
    }
  }
  //cout<<s<<endl;
  return true;
  
}
int main(void){
  string str;
  int n;
  cin>>n;
  while(n--){
    cin>>str;
    if(A(str))cout<<"A"<<endl;
    else if(B(str))cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
}

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 j=2;
	  while(s[j++]=='Q'&&s[j++]=='=');
	  j--;
      if(s[j++]!='~'||s[j++]!='~')ans="NA";
	  if(j!=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 <algorithm>

using namespace std;

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[0] != '>') NA = 1;
      if(s[1] == '\'') {
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	if( !(s[2] == 'Q' && s[3] == '=') ) NA = 1;
	int cnt;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[1] == '\'') {
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	if( !(s[2] == 'Q' && s[3] == '=') ) NA = 1;
	int cnt;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        string sn;
        cin>>sn;
        int type=0;
        if(sn[0]=='>'&&sn[1]==39)type=1;
        if(sn[0]=='>'&&sn[1]=='^')type=2;
        int flg=1;
        if(type==1){
            int cou=0;
            int aftercou=0;
            int p;
            for(int j=2;j<sn.size();j++){
                if(sn[j]=='=')cou++;
                else if(sn[j]=='#'){
                    p=j;
                    break;
                }
                else{
                    flg=0;
                    goto syutu;
                }
            }
            for(int k=p+1;k<sn.size();k++){
                if(sn[k]=='=')aftercou++;
                else if(sn[k]=='~'&&k==sn.size()-1){
                    if(cou!=aftercou)flg=0;
                    goto syutu;
                }
                else{
                    flg=0;
                    goto syutu;
                }
            }
            if(sn[sn.size()-1]!='~')flg=0;
syutu:
            if(flg)puts("A");
            else puts("NA");
        }
        else if(type==2){
            for(int j=2;j<sn.size()-1;j+=2){
                if(sn[j]=='~'&&sn[j+1]=='~')break;
                if(!(sn[j]=='Q'&&sn[j+1]=='='))flg=0;
            }
            if(sn[sn.size()-2]!='~'||sn[sn.size()-1]!='~')flg=0;
            if(flg)puts("B");
            else puts("NA");
        }
        else puts("NA");
    }
    return 0;
}