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<cstdio>
#include<algorithm>
using namespace std;

#define rep(i, n)for((i) = 0;(i) < (n);++(i))

string solve(string s){
	if(s[0] != '>')return "NA";
	if(s.size() % 2 != 0)return "NA";
	
	int i = 0, j;
	if(s[1] == '\''){
		for(i = 2;i < s.size();++i){
			if(s[i] == '#')break;
			else if(s[i] != '=')return "NA";
		}
		int l = i - 2; ++i;
		rep(j, l)if(s[i + j] != '=')return "NA";
		if(s[s.size() - 1] != '~')return "NA";
		if(i + l + 1 != s.size())return "NA";
		return "A";
	}else if(s[1] == '^'){
		for(i = 2;i < s.size();i+=2){
			if(s.substr(i, 2) != "Q="){
				if(i + 2 == s.size() && s.substr(i, 2) == "~~")break;
				else return "NA";
			}
		}
		return "B";
	}
	return "NA";
}

int main(void){
	int i, j, n;
	string s;
	scanf("%d", &n);
	for(;n--;){
		cin >> s;
		cout << solve(s) << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
  int n,size;
  string s,a,b;
  for(cin>>n,getline(cin,s,'\n');n>0;n--){
    getline(cin,s,'\n');
    //cin>>s;
    size = s.size();
    if(size%2){cout<<"NA"<<endl;continue;}
    a=">'";
    b=">^";
    for(int i=0;i<size/2-2;i++){a=a+"=";b=b+"Q=";}
    a+="#";
    for(int i=0;i<size/2-2;i++){a=a+"=";}
    a+="~";
    b+="~~";
    if (s==a)cout<<"A"<<endl;
    else if (s==b)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  int n;

  cin >> n;
  while(n--){
    string snake;
    cin >> snake;

    string s = snake.substr(0,2);
 

    if(s != ">^" && s != ">\'"){
      puts("NA");
      continue;
    }

    if(s == ">\'"){
      int a = 0;
      int p = 2;
      bool flg = true;
      int len = (int)snake.size();
      for(int i = 2 ; i < len ; i++,p++){
	if(snake[i] == '#') break;
	else if(snake[i] == '=') a++;
	else{
	  flg = false;
	  break;
	}
      }

      if(!flg){
	puts("NA");
	continue;
      }      

      int b = 0,q = p + 1;
      for(int i = q ; i < len ; i++,p++){
	if(snake[i] == '~') break;
	else if(snake[i] == '=') b++;
	else{
	  flg = false;
	  break;
	}
      }

      if(!flg){
	puts("NA");
	continue;
      }  

      if(a == b && snake[len-1] == '~'){
	puts("A");
      }else{
	puts("NA");
      }
    }else{
      bool ok = true;
      int p = 0;
      string ss = "";
      int len = (int)snake.size();

      for(int i = 2 ; i < len-2 ; i++,p++){
	ss += snake[i];
	if(p == 1){
	  p = -1;
	  if(ss != "Q="){
	    ok = false;
	    break;
	  }
	  ss = "";
	}
      }

      if(snake[len-2] == '~' && snake[len-1] == '~' && ok){
	puts("B");
      }else{
	puts("NA");
      }
    }
  }

  return 0;
}

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

bool isa(string s) {
    int pos = 2,cnt = 0;
    if(s[pos] != '=') return false;
    while(s[pos] == '=') pos++,cnt++;
    if(s[pos] != '#') return false;
    pos++;

    for(int i=0; i<cnt; ++i) {
        if(s[pos+i] != '=') return false;
    }
    pos += cnt;
    if(pos == s.length()-1 && s[pos] == '~') return true;
    return false;
}

bool isb(string s) {
    int pos = 2;
    if(s[2] != 'Q' || s[3] != '=') return false;
    for(; pos < s.length()-2; pos += 2)
        if(s[pos] != 'Q' || s[pos+1] != '=') return false;

    if(s[s.length()-1] == s[s.length()-2] && s[s.length()-1] == '~') return true;
    return false;
}

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

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

        if(s.length() <= 2 || s[0] != '>') {
            cout<<"NA"<<endl;
        } else if(s[1] == '\'') {
            if(isa(s)) {
                cout<<"A"<<endl;
            }else{
                cout<<"NA"<<endl;
            }
        } else {
            if(isb(s)) {
                cout<<"B"<<endl;
            }else{
                cout<<"NA"<<endl;
            }
        }
    }
}

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;
	  continue;
	}
	tale++;
      }

      if(head == tale && snake[snake.size()-1] == '~'){
	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 D = 0; D < n; ++D){
		string s;
		cin>>s;
		
		if(s.compare(0,2,">'")== 0){
			int bef_eq = 0;
			int aft_eq = 0;
			int shp = 0;
			int pos_shp = 0;
			int pos_tld = 0;

			for(unsigned int i = 2; i < s.length() && pos_tld == 0; ++i ){
				switch( s[i] ){
					case '=':
						if( shp == 0 )
							++bef_eq;
						else
							++aft_eq;
						break;
					case '#':
						shp++;
						break;
					case '~':
						pos_tld = i;
						break;
					default:
						shp = 2;
						break;
				}
			}
			if( bef_eq && aft_eq && pos_tld && bef_eq == aft_eq && shp == 1 )
				cout << "A\n";
			else
				cout << "NA\n";
		}else if(s.compare(0,2,">^")==0){
			int cnt_qeq = 0;
			int pos_tltl = 0;
			bool invalid = false;
			
			if( s.length() <= 2 || s.length() % 2 == 1 )
				invalid = true;
			
			for(unsigned int i = 2; i < s.length() && !invalid; i+=2){
				if( s.compare(i,2,"Q=") == 0 )
					++cnt_qeq;
				else if( s.compare(i,2,"~~") == 0 ){
					pos_tltl = i;
					break;
				}else
					invalid = true;
			}
			if( pos_tltl != s.length() - 2 ){
				invalid = true;
			}
			if( invalid )
				cout << "NA\n";
			else
				cout << "B\n";
		}else{
			cout <<"NA\n";
		}
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

int main(){
  int n;
  cin >> n;
  while(n-- > 0)
    {
      string s;
      cin >> s;
      cerr << s << endl;
      cout << "A" << endl;
    }


  return 0;
}