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 <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


bool checkA( string s, int k, bool f0, bool f1, bool f2 ) {
    int n = s.size();
    if ( k == n - 1 ) {
        if ( s[k] != '~' ) return false;
        return f0 && f1 && f2;
    }
    if ( k == 0 ) {
        if ( n < 2 ) return false;
        if ( s[k] != '>' || s[k+1] != '\'' ) return false;
        return checkA( s, k + 2, f0, f1, f2 );
    }
    if ( ! f0 ) {
        if ( s[k] != '=' ) return false;
        return checkA( s, k + 1, true, f1, f2 );
    }
    if ( ! f1 ) {
        if ( s[k] == '#' ) {
            return checkA( s, k + 1, f0, true, f2 );
        } else if ( s[k] == '=' ) {
            return checkA( s, k + 1, f0, f1, f2 );
        }
        return false;
    }
    if ( ! f2 ) {
        if ( s[k] != '=' ) return false;
        return checkA( s, k + 1, f0, f1, true );
    }
    return s[k] == '=' && checkA( s, k + 1, f0, f1, f2 );
}

bool checkB( string s, int k ) {
    int n = s.size();
    if ( k == 0 ) {
        if ( n < 2 ) return false;
        if ( s[k] != '>' || s[k+1] != '^' ) return false;
        return checkB( s, k + 2 );
    }
    if ( s[k] == 'Q' ) {
        if ( k + 1 >= n ) return false;
        if ( s[k+1] != '=' ) return false;
        return checkB( s, k + 2 );
    }
    if ( k + 2 == n && s[k] == '~' ) {
        return s[k+1] == '~';
    }
    return false;
}

string solve( string s ) {
    if ( checkA( s, 0, false, false, false ) ) return "A";
    if ( checkB( s, 0 ) ) return "B";
    return "NA";
}

int main() {
    int n;
    cin >> n;
    for ( int i = 0; i < n; ++ i ) {
        string s;
        cin >> s;
        cout << solve( s ) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	string s;
	int n;
	cin>>n;
	rep(i,n){
		cin>>s;
		int size=s.size();
		string A="";
		string B="";
		rep(i,size){
			if(i==0)A+=">";
			else if(i==1)A+="'";
			else if(i==(size-1))A+="~";
			else if(i==((size-3)/2+2))A+="#";
			else A+="=";
		}
		rep(i,size){
			if(i==0)B+=">";
			else if(i==1)B+="^";
			else if(i>=size-2)B+="~";
			else if(i%2==0)B+="Q";
			else B+="=";
		}
		if(size%2==0){
			if(s==A)cout<<"A"<<endl;
			else if(s==B)cout<<"B"<<endl;
			else cout<<"NA"<<endl;
		}else cout<<"NA"<<endl;
	}
	return 0;
}

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

int main(){

  int in;

  cin >> in;

  for(int i = 0; i < in; i++){
    int status = 0;
    string snake;
    cin >> snake;
    if(snake[0] != '>') {
      cout << "NA" << endl;
      continue;
    }
    if(snake[1] == '^'){

      for(int j = 2; j < snake.size()-2; j+=2){
	if(snake[j] != 'Q' || snake[j+1] != '='){
	  cout << "NA" << endl;
	  status = 1;
	  break;
	}
      }
      if(snake[snake.size()-2] == '~' && snake[snake.size()-1] == '~' && status == 0){
	cout << "B" << endl;
      }
      continue;
    }else if(snake[1] == '\''){
      int head = 0, tale = 0, join;

      for(int j = 2; j < snake.size(); j++){
	if(snake[j] == '#'){
	  join = j;
	  break;
	}
	head++;
      }

      for(int j = join+1; j < snake.size()-1; j++){
	if(snake[j] != '='){
	  cout << "NA" << endl;
	  status = 1;
	  break;
	}
	tale++;
      }

      if(head == tale && snake[snake.size()-1] == '~' && status == 0){
	cout << "A" << endl;
	continue;
      }
    }
    cout << "NA" << endl;
  }

  return 0;
}

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

int main() {
  int n; cin >> n;
  for(int i = 0; i < n; i++) {
    string s; cin >> s;
    bool a[4] = {}, b[3] = {};
    int cnt = 0;
    for(int j = 0; j < s.size(); j++) {
      if(s[j] == '>') {
        if(!a[1] && s[j+1] == ''') a[0] = true;
        if(s[j+1] == '^') b[0] = true;
      } else if(a[0] && s[j] == '=') {
        a[1] = true;
        cnt++;
      } else if(b[0] && s.substr(j,2) == "Q=") {
        b[1] = true;
      } else if(a[1] && s[j] == '#') {
        a[0] = false;
        a[2] = true;
      } else if(a[2] && s[j] == '=') {
        cnt--;
      } else if(a[1] && cnt == 0 && j+1 == s.size() && s[j] == '~') {
        a[3] = true;
      } else if(b[1] && j+2 == s.size() && s.substr(j,2) == "~~") {
        b[2] = true;
      }
    }

    if(a[3]) cout << 'A' << endl;
    else if(b[2]) cout << 'B' << endl;
    else cout << "NA" << endl;
  }
}

C++: #include<iostream>
using namespace std;
int main()
{
  int n;
  for(cin>>n;n--;)
    {
      string s;
      cin>>s;
      string ans="NA";

      if(s.substr(0,2)==">'" &&s[s.size()-1]=='~')
	{
	  string tmp=s.substr(2,s.size()-3);
	  cout<<tmp<<endl;
	  if(tmp.size()%2!=0 && tmp[tmp.size()/2]=='#' &&tmp.substr(0,tmp.size()/2)==tmp.substr(tmp.size()/2+1,tmp.size()/2))
	    {
	      ans="A";
	    }
	}
      else if(s.substr(0,2)==">^" && s.substr(s.size()-2,2)=="~~")
	    {
	      string tmp=s.substr(2,s.size()-4);
	      //	      cout<<tmp<<endl;
	      while(tmp.size()>0)
		{
		  if(tmp.substr(0,2)=="Q=")
		      tmp=tmp.substr(2,tmp.size()-2);
		  else
		    break;
		}
	      if(tmp.size()==0)
		{
		  ans="B";
		}
	    }
	  cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
string s;

int main(){
	cin>>n;
	while(cin>>s){
		if(s.size()<6||s[0]!='>'){cout<<"NA"<<endl;break;}
		int cur=2,l=0,l2=0,f=0,f2=0;
		if(s[1]=='\''){
			while(cur<s.size()){
				if(f2){f2=0;break;}
				else if(s[cur]=='='){
					if(!f)l++;
					else l2++;
				}
				else if(s[cur]=='#'){
					if(!l||f)break;
					else f=1;
				}
				else if(s[cur]=='~'){
					if(l&&l==l2){f2=1;}
					else break;
				}
				else break;
				cur++;
			}
		}
		else if(s[1]=='^'){
			while(cur<s.size()){
				if(f2){f2=0;break;}
				if(s[cur]=='Q'){
					if(!f)f=1;
					else break;
				}
				else if(s[cur]=='='){
					if(f==1)f=0;
					else break;
				}
				else if(s[cur]=='~'){
					if(!f)f=2;
					else if(f==2)f2=2;
					else break;
				}
				else break;
				cur++;
			}
		}
		if(f2==1)cout<<"A"<<endl;
		else if(f2==2)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
}