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 <algorithm>
#include <string>
using namespace std;
int n;
string s;
int main(void){
  cin >> n;
  for(int k = 0; k < n; k++){
    cin >> s;
    bool f = true;
    if(s[0]== '>' && s[1]=='\''){
      int c=0;
      int i;
      for(i = 2; s[i]=='='; i++) c++;
      if(c && s[i]=='#'){
	for(i += 1; s[i]=='='; i++) c--;
	if(!c && (int)s.size() == i+1 && s[i]=='~'){
	  cout << 'A' << endl;
	  f = false;
	}
      }
    }
    else if(s[0]== '>' && s[1]=='^' && s[2]=='Q' && s[3]=='='){
      int i;
      for(i = 4; s[i]=='Q' && s[i+1]=='='; i+=2);
      if((int)s.size() == i+2 && s[i]=='~' && s[i+1]=='~'){
	  cout << 'B' << endl;
	  f = false;
      }
    }
    if(f){
      cout << "NA" << endl;
    }
  }
}

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

using namespace std;

int main(void){

  string s;
  int n;
  cin >> n;

  while(n--){

    cin >> s;
    if( s[0] == '>' ){
      if( s[1] == '\'' ){
	int cnt1 = 0;
	int i;
	for( i = 2 ; s[i] == '=' ; i++ ){
	  cnt1++;
	}
	if( s[i] != '#' ){
	  cout << "NA" << endl;
	  goto skip;
	}
	else i++;
	int cnt2 = 0;
	for( ; s[i] == '=' ; i++ ){
	  cnt2++;
	}
	if( cnt1 == cnt2 && s[i] == '~' && i + 1 == s.size() )
	  cout << "A" << endl;
	else cout << "NA" << endl;
      }
      else if( s[1] == '^' ){
	int x = 2;
	while( s[x] == 'Q' && s[x + 1] == '=' ) x += 2;
	if(s[x] == '~' && s[x + 1] == '~' && x + 2 == s.size() ){
	  cout << "B" << endl;
	}
      }
      else cout << "NA" << endl;
    }
  skip:;
  }
  return 0;
}

C++: #include<stdio.h>
#include<string.h>

bool checkA(char st[1001]){
	int len = strlen(st);
	if(len<4)return false;
	if(st[len-1]!='~')return false;
	if(len%2 == 1)return false;
	st[len/2-1] = '=';
	for(int i = 0; i < len-1; i++){
		if(st[i] !='=')return false;
	}
	return true;
	
}
bool checkB(char st[1001]){
	if(strlen(st)<4)return false;
	if(st[strlen(st)-1] != '~' || st[strlen(st)-2] != '~')return false;
	for(int i = 0 ; i < strlen(st)-2; i+=2){
		if(!(st[i]=='Q' && st[i+1] =='='))return false;
	}
	return true;
}


int main(void){
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n ;i++){
		char str[1001];
		scanf("%s",str);
		
		bool Af = false;
		bool Bf = false;
		
		if(str[1] == '\''){
			Af = checkA(str+2);
		}else {
			Bf = checkB(str+2);
		}
		if(str[0] != '>'){
			puts("NA");
		}else if(Af == Bf){
			puts("NA");
		}else if(Af == true){
			puts("A");
		}else{
			puts("B");
		}
	}
	return 0;
}

C++: // 2011/10/02 Tazoe

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int n;
	cin >> n;

	for(int i=0; i<n; i++){
		string str;
		cin >> str;

		if(str[0]!='>'){
			cout << "NA" << endl;
			continue;
		}

		if(str[1]=='\''){
			int N = 0;
			int I = 2;
			while(str[I]=='='){
				N++;
				I++;
			}
			if(N==0){
				cout << "NA" << endl;
				continue;
			}

			if(str[I]!='#'){
				cout << "NA" << endl;
				continue;
			}
			I++;

			int M = 0;
			while(str[I]=='='){
				M++;
				I++;
			}
			if(N!=M){
				cout << "NA" << endl;
				continue;
			}

			if(!(str[I]=='~'&&I+1==str.size())){
				cout << "NA" << endl;
				continue;
			}

			cout << "A" << endl;
		}
		else if(str[1]=='^'){
			int N = 0;
			int I = 2;
			while(str[I]=='Q'&&str[I+1]=='='){
				N++;
				I += 2;
			}
			if(N==0){
				cout << "NA" << endl;
				continue;
			}

			if(!(str[I]=='~'&&str[I+1]=='~'&&I+2==str.size())){
				cout << "NA" << endl;
				continue;
			}

			cout << "B" << endl;
		}
		else
			cout << "NA" << endl;
	}

	return 0;
}

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

using namespace std;

string afunc(string str)
{
	string res = "NA";
	
	for(int i = 0, j = str.size()-1; i <= j; i++, j--){
		if(i == j){
			if(str[i] == '#'){
				res = "A";
			}
		} else if(str[i] != str[j]){
			break;
		}
	}
	
	return res;
}

string bfunc(string str)
{
	string res = "B";
	
	for(int i = 0; i < str.size(); i+=2){
		if(str[i] != 'Q' || str[i+1] != '='){
			res = "NA";
			break;
		}
	}
	
	return res;
}

int main()
{
	int n;
	string str, res;
	
	while(cin >> n && n){
		while(n--){
			cin >> str;
			res = "NA";
			if('>' == str[0]){
				str.erase(str.begin());
				int l = str.size();
				if(str[0] == '\'' && str[l-1] == '~'){
					str.erase(str.end()-1);
					str.erase(str.begin());
					if(str.size()%2 == 1){
						res = afunc(str);
					}
				} else if(str[0] == '^' && str[l-1] == str[l-2] && str[l-1] == '~'){
					str.erase(str.end()-1);
					str.erase(str.end()-1);
					str.erase(str.begin());
					
					if(str.size()%2 == 0){
						res = bfunc(str);
					}
				}
			}
			
			cout << res << endl;
		}
	}
	
	return 0;
}

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

int main(){
  
  int n;
  cin >> n;
  while(n--){
    bool judge = false;
    string s; cin >> s;
    if(s[0] == '>'){
      if(s[1] == '\'' && s[s.size()-1] == '~'){
	if(s[s.size() / 2] == '#' && !(s.size() & 1) && s.size() >= 6)  cout << "A" << endl;
	else judge = true;
      }
      else if(s[1] == '^' && s[s.size()-1] == s[s.size()-2] && s[s.size()-1] == '~'){
	for(int i = 2; i < s.size()-2; i++){
	  if((!(i & 1) && s[i] != 'Q') || (i & 1 && s[i] != '=') || s.size() < 6) judge = true;
	}
	if(!judge) cout << "B" << endl;
      }
      else judge = true;
    }
    else judge = true;
    if(judge) cout << "NA" << endl;
  }

}

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