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 <string>
using namespace std;

bool isSnakeA(string snake) {
    if (snake.substr(0, 2) != ">'" || snake[snake.length() - 1] != '~') {
        return false;
    }
    snake.erase(snake.begin(), snake.begin() + 2);
    snake.erase(snake.end() - 1, snake.end());
    size_t pos = snake.find('#');
    if (pos == string::npos || snake.substr(0, pos) != snake.substr(pos + 1, snake.length() - pos)) {
        return false;
    }
    return true;
}

bool isSnakeB(string snake) {
    if (snake.substr(0, 2) != ">^" || snake.substr(snake.length() - 2, 2) != "~~") {
        return false;
    }
    snake.erase(snake.begin(), snake.begin() + 2);
    snake.erase(snake.end() - 2, snake.end());
    if (snake.length() % 2 != 0) {
        return false;
    }
    while (snake.empty() == false) {
        if (snake.substr(0, 2) != "Q=") {
            return false;
        }
        snake.erase(snake.begin(), snake.begin() + 2);
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        string snake;
        cin >> snake;
        if (isSnakeA(snake)) {
            cout << "A" << endl;
        } else if (isSnakeB(snake)) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}

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

int main() {
  int n;
  cin >> n;
  getchar();
  for(int i = 0; i < n; i++) {
    string snake;
    getline(cin, snake);

    string ans = "NA";
    if(snake[0] == '>' && snake[1] == '\'') {
      int shape = 0;
      for(int i = 2; i < snake.length(); i++) {
	if(i+1 >= snake.length()) {
	  if(snake[i] == '~') ans = "A";
	} else {
	  if(snake[i] != '=' && snake[i] != '#') break;
	  if(snake[i] == '#') {
	    if(shape == 1) break;
	    else {
	      shape++;
	      if(i-2 != (double)(snake.length()-4)/2) break;
	    }
	  }
	}
      }
    } else if(snake[0] == '>' && snake[1] == '^') {
      for(int i = 3; i < snake.length(); i+=2) {
	if(i+2 >= snake.length()) {
	  if(snake[i-1] == '~' && snake[i] == '~') ans = "B";
	} else {
	  if(snake[i-1] != 'Q' || snake[i] != '=') break;
	}
      }
    }

    cout << ans << endl;
  }
  return 0;
}

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

#define f first
#define s second
#define mp make_pair

#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()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;

bool isA(const string &str){
  int n = str.size();
  int m = 0;
  int pos = 2;
  if(n == 0 || n==1) return false;
  if(str[0] != '>') return false;
  if(str[1] != '\'') return false;
  while(pos < n && str[pos] == '=') {m++; pos++;}
  if(pos == n || m == 0) return false;
  if(str[pos++] != '#') return false;
  while(pos < n && str[pos] == '=') {m--; pos++;}
  if(pos == n) return false;
  if(str[pos++] != '~') return false;
  if(m == 0 && pos == n) return true;
  return false;
}

bool isB(const string &str){
  int n = str.size();
  int pos = 2;
  int m = 0;
  if(n == 0 || n == 1) return false;
  if(str[0] != '>') return false;
  if(str[1] != '^') return false;
  while(pos+1 < n && str[pos] == 'Q' && str[pos+1] == '=') {m++; pos+=2;}
  if(m == 0) return false;
  if(pos+2 != n) return false;
  if(str[pos] == '~' && str[pos+1] == '~') return true;
  return false;
}

int main(){
  int n;
  string str;
  getline(cin,str); sscanf(str.c_str(),"%d",&n);
  while(n --> 0){
    getline(cin,str);
    if(isA(str)) puts("A");
    else if(isB(str)) puts("B");
    else puts("NA");
  }
  return 0;
}

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){
      while(idx < str.size() && str[idx] != '#') {
	cnt++;
	idx++;
      }
      idx++;
      while(idx < str.size() && str[idx] != '~') {
	cnt--;
	idx++;
      }
      if(cnt == 0) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(idx+1 < str.size() && str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
      }
      if(str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n;
  rep(i,n){
    cin >> s;
    if(s[0] != '>'|| s.size() < 6|| (s[1] != '\'' && s[1] != '^') ){
      cout << "NA"<<endl;
    }
    else if(s[1] == '\''){
      int cnt = find(all(s),'#') - s.begin()-2;
      if(s[2*cnt+3] == '~' && 2*cnt+3 == s.size()-1){
        cout << "A"<<endl;
      }
      else{
        cout <<"NA"<<endl;
      }
    }
    else{
      bool f = false;
      for(int i = 2; i < s.size(); i+=2){
        if(!(s[i] == 'Q' && s[i+1] == '=')){
          if(s[i] == '~' && s[i+1] == '~' && i == s.size()-2){
            cout << "B"<<endl;
            f = true;
          }
          else{
            break;
          }
        }
      }
      if(!f) cout << "NA"<<endl;
    }
  }
  return 0;
}