Problem:
ヘビ
ある世界には，文字だけでできた不思議なヘビが住んでいます。このヘビには現在A種とB種の2種類が確認されていますが、それ以外の種類がいる可能性もあります。
A種は，">'"の後に"="が1個以上並んだ後、"#"が来て、さらに前と同じ個数の"="が来た後、"~"（半角チルダ）で終わります。
B種は，">^"の後に "Q="が1個以上並んだ後、"~~"で終わります。
A種の例： >'====#====~        >'==#==~
B種の例： >^Q=Q=Q=Q=~~        >^Q=Q=~~
ヘビを文字列データとして受け取り、それがどんな種類であるかを判別して、A種の場合は「A」、B種の場合は「B」、それ以外の種類の場合は「NA」を出力するプログラムを作成してください。
Input
入力は以下の形式で与えられます。
n
S
1
S
2
:
S
n
1 行目に判別されるヘビの数
n
（1 ≤
n
≤ 10000）、続く
n
行に
i
匹目のヘビを表す文字列
S
i
(200文字以下の、空白を含まない文字列) がそれぞれ１行に与えられます。
Output
i
行目に
i
匹目のヘビの種類 A、B または NA を出力してください。
Sample Input
3
>'======#======~
>^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~
>'===#====~
Output for the Sample Input
A
B
NA

Submitted solutions:

code_snippet: #include<iostream>
#include<string>
#include<regex>
using namespace std;
int main(){
string A = ">'(=+)#\\1~$";
string B = ">\\^(Q=)+~~$";
int n; cin >> n;
for ( int i = 0; i < n; i++ ) {
string snake; cin >> snake;
if( regex_search(snake, regex(A)) )
cout << "A" << endl;
else if( regex_search(snake, regex(B)) )
cout << "B" << endl;
else
cout << "NA" << endl;
}
return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3328
final_rank: 18

code_snippet: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

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

string solve(string s){
	int i = 0, j;
	string tmp;
	rep(i, 100){
		tmp += "=";
		if(s == ">'" + tmp + "#" + tmp + "~")return "A";
	}
	tmp.clear();
	rep(i, 100){
		tmp += "Q=";
		if(s == ">^" + tmp + "~~")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;
}
accuracy: 1
time_ms: 30
memory_kb: 3240
final_rank: 19

code_snippet: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<regex>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int n; cin >> n;
	regex b(R"(>\^(Q=)+~~)");
	rep(i, 0, n) {
		string s; cin >> s;
		int m = 0;
		rep(j, 0, s.size())if (s[j] == '=')m++;
		regex a(">'={" + to_string(m / 2) + "}#={" + to_string(m / 2) + "}~");
		if (regex_match(s, a) && m) { cout << "A" << endl; }
		else if (regex_match(s, b)) { cout << "B" << endl; }
		else cout << "NA" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3428
final_rank: 20

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

bool asnake(string s){
	int n=s.size();
	if(n%2 || n<6)return false;
	string com=">\'";
	rep(i,(n-4)/2)com+='=';
	com+='#';
	rep(i,(n-4)/2)com+='=';
	com+='~';
	if(s==com)return true;
	else return false;
}

bool bsnake(string s){
	int n=s.size();
	if(n%2 || n<6)return false;
	string com=">^";
	rep(i,(n-4)/2)com+="Q=";
	com+="~~";
	if(s==com)return true;
	else return false;
}

int main(){
	int n;
	cin>>n;
	rep(p,n){
		string s;
		cin>>s;
		if(asnake(s))cout<<"A"<<endl;
		else if(bsnake(s))cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 12

code_snippet: #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.size()%2!=0||s.size()<6)a=false,b=false;
        if(s[1]!='\''||s[s.size()-1]!='~')a=false;
        if(s[1]!='^'||s[s.size()-1]!='~'||s[s.size()-2]!='~')b=false;
        if(a){
            if(s[s.size()/2]!='#')a=false;
            int count=0;
            for(int i=2;i<s.size()-1;i++)if(s[i]!='=')count++;
            if(count!=1)a=false;
            if(a)cout<<"A"<<endl;
        }
        if(b){
            for(int i=2;i<s.size()-2;i++){
                if(i%2==0&&s[i]!='Q')b=false;
                if(i%2==1&&s[i]!='=')b=false;
            }
            if(b)cout<<"B"<<endl;
        }
        if(!a&&!b)cout<<"NA"<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1200
final_rank: 3

code_snippet: #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&&cou)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]=='~'&&j!=2)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;
}
accuracy: 1
time_ms: 10
memory_kb: 1180
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

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

	while(n--)
	{
		string str;
		string kp;
		string set;
		int flag=0;
		int count=0;
		int count_b=0;
		int i=0;

		cin >> str;
		for(i=0;i<2;i++)kp+=str[i]++;
		
		if(kp==">'")
		{
			while(str[i]!='#')
			{
				if(str[i]=='=')count++;
				else flag=-1;

				if(flag==-1)break;
				i++;	
			}
			i++;
			while(str[i]!='~')
			{
				if(str[i]=='=')count_b++;
				else flag=-1;

				if(flag==-1)break;
				i++;
			}
			if(flag!=-1 && count==count_b && count>=1)flag=1;

			count=0;
			for(int j=i;j<str.length();j++)
			{
				if(str[i]!='~')flag=-1;
				else count++;

				if(flag==-1)break;
			}
			if(count!=1)flag=-1;
		}
		else if(kp==">^")
		{
			while(i<str.length()-2)
			{
				count=2;
				while(count--)
				{
					set+=str[i];
					i++;
				}
				if(set=="Q=")flag=2;
				else flag=-1;

				set.erase(set.begin(),set.end());
				if(flag==-1)break;
			}
			for(i=str.length()-2;i<str.length();i++)set+=str[i];
			if(set=="~~" && flag==2)flag=2;
			else flag=-1;
		}

		if(flag==1) cout << "A" << endl;
		else if(flag==2)cout << "B" << endl;
		else cout << "NA" << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1212
final_rank: 11

code_snippet: #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;
}
accuracy: 1
time_ms: 20
memory_kb: 1224
final_rank: 17

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

bool isOutOfStr(int ind, string str){
  return ind >= str.length();
}

int main(void){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    if(!isOutOfStr(1, str) &&str[0] == '>' && str[1] == '\''){
      int sharp_cnt = 0;
      int ind;
      ind = 2;

      if(isOutOfStr(ind, str) || str[ind] != '='){
        cout << "NA" << endl;
        continue;
      }

      for(;;ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          break;
        }
        sharp_cnt++;
      }

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

      bool flg = true;
      for(int i=0; i < sharp_cnt; i++, ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          cout << "NA" << endl;
          flg = false;
          break;
        }
      }
      if(!flg) continue;

      if(!isOutOfStr(ind, str) && str[ind] == '~' && ind == str.length() - 1)
        cout << "A" << endl;
      else{
        cout << "NA" << endl;
      }



    }else if(str[0] == '>' && str[1] == '^'){
      int ind;
      ind=2;

      if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
        cout << "NA" << endl;
      }else{
        int flg = true;
        for(;;ind+=2){
          if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
            break;
          }
        }

        if(!isOutOfStr(ind, str) && !isOutOfStr(ind+1, str) && str[ind] == '~' && str[ind+1] == '~' && ind+1 == str.length() - 1){
          cout << "B" << endl;
        }else{
          cout << "NA" << endl;
        }
      }
    }else{
      cout << "NA" << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 6

code_snippet: #include <iostream>
#include <string>

using namespace std;

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

  for(int q=0;q<n;q++) {
    string str;
    cin >> str;
    if(str[0] != '>') {
      cout <<"NA"<<endl; 
      continue;
    }  else if(str[1] != '\''&& str[1] != '^') {
      cout << "NA" << endl;
      continue;
    }
    int flg = 0,sum;
    if(str[1] == '\'') {
      int t = 0;
      sum=0;
      for(int i=2;i<str.size();i++) {
	if(str[i] == '=') t++;
	else if(str[i] == '#' && sum==0 && i!=2) sum = t , t = 0;
	else break;
	if(str[str.size()-1] == '~' && i == str.size() -2 && sum==t ) flg = 1;
	
      }
    }

      if(str[1] == '^' && flg == 0) {
	flg = 0;
	for(int i=2;i<str.size() ;i++) {
	  if(i%2 == 0 && str[i] != 'Q') break;
	  if(i%2 == 1 && str[i] !='=')  break;

	  if(str[str.size()-2] == '~' && str[str.size()-1] =='~' && i == str.size()-3&& i%2==1 ){
	    flg = 2;
	    break;
	  }
	}
      }

      if(flg == 0) cout << "NA" << endl;
      else if(flg == 1)cout <<"A" << endl;
      else if(flg == 2) cout << "B"<<endl;
  }     


  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 6

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

bool A(string s){
  if(s.size() < 6) return false;
  if(s.size()%2 != 0)return false;
  if(s[0] != '>')return false;
  if(s[1] != '\'')return false;
  if(s[s.size()-1] != '~')return false;
  if(s[s.size()/2] != '#')return false;
  else s[s.size()/2] = '=';
  loop(i,2,s.size()-1){
    if(s[i] != '=')return false;
  }
  return true;
}
bool B(string s){
  if(s.size() < 6) return false;
  if(s[0] != '>')return false;
  if(s[1] != '^')return false;
  loop(i,2,s.size()-2){
    if(i%2){
      if(s[i] != '=')return false;
    }else{
      if(s[i] != 'Q')return false;
    }
  }
  if(s[s.size()-2] != '~')return false;
  if(s[s.size()-1] != '~')return false;
  return true;
}
int main(void){
  int n;
  cin>>n;
  rep(i,n){
    string s;
    cin>>s;
    if(A(s))cout<<"A";
    else if(B(s))cout<<"B";
    else cout<<"NA";
    cout<<endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 6

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
	int n; cin >> n;
	rep(q, n){
		string s; cin >> s;
		if(s.substr(0, 2) == ">'"){
			s = s.substr(2);
			int c = 0;
			bool p = true;
			for(int i=0; i < s.size(); i++) if(s[i] == '='){c++;} else break;
			if(s[c] != '#' || c == 0)p = false;
			s = s.substr(c+1);
			if(c+1 != s.size() || s[s.size()-1] != '~')p = false;
			rep(i, c) if(s[i] != '=')p = false;
			cout << (p ? "A" : "NA") << endl;
		}else if(s.substr(0, 2) == ">^"){
			bool p = true;
			s = s.substr(2);
			if(s.substr(0, 2) != "Q=") p = false;
			while(s.substr(0, 2) == "Q=") s = s.substr(2);
			cout << (s == "~~" && p ? "B" : "NA") << endl;
		}else {
			cout << "NA" << endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 12

code_snippet: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

string solve(string s){
    if(s.substr(0,2)==">'"){
        int eq=0;
        for(int i=2;i<s.size();i++){
            if(s[i]!='=')break;
            else{
                eq++;
            }
        }
        
        if(eq==0)return "NA";
        
        if( s[ (2 + eq -1) +1 ] != '#')return "NA";
        
        if( s.substr( ((2+eq-1)+1) +1 ,eq )   !=   s.substr( 2,eq ) )return "NA";

        if( 2+eq+1+eq+1 == s.size() && s[ (2+eq+1+eq+1) -1 ]=='~')return "A";
        
    }else if(s.substr(0,2)==">^"){
        int c=2;
        while( s[c]=='Q' && s[c+1]=='=' ){
            c+=2;
        }
        if(c==2)return "NA";
        
        
        if( c+1==s.size()-1 && s[c]=='~' && s[c+1]=='~')return "B";
        else return "NA";
        
        
    }else return "NA";

    return "NA";
}


int main(){
    int n;
    cin>>n;
    rep(i,n){
        string s;
        cin>>s;
        
        cout<<solve(s)<<endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 12

code_snippet: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

bool isA(string s){
	int n = 0;
	if(s.at(n)!='>' || s.at(++n)!='\'' ||s.at(++n)!='=') return false;
	int cnt = 1;
	for(;s.at(++n)=='='; cnt++);
	if(s.at(n)!='#') return false;
	for(int i=0; i<cnt; i++){
		if(s.at(++n)!='=') return false;
	}
	if(s.at(++n)!='~' || s.size()>n+1) return false;
	return true;
}

bool isB(string s){
	int n = 0;
	if(s.at(n)!='>' || s.at(++n)!='^' || s.at(++n)!='Q' || s.at(++n)!='=') return false;
	while(true){
		if(s.at(++n)=='Q'){
			if(s.at(++n)!='=') return false;
		}
		else break;
	}
	if(s.at(n)!='~' || s.at(++n)!='~' || s.size()>n+1) return false;
	return true;
}

int main(){
	string S;
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>S;
		if(isA(S)) puts("A");
		else if(isB(S)) puts("B");
		else puts("NA");
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1188
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    int N;
    cin>>N;
    while(N--){
        string str;
        cin>>str;
        int len=str.size();
        int ans=-1;
        if(str[0]=='>'&&str[1]=='\''){
            int pos=2;
            int cnt=0;
            int turn=0;
            int flag=0;
            do{
                if(str[pos]=='=')
                    cnt+=(turn)?(-1):(1);
                else if(str[pos]=='#')turn=1;
                else flag=0;
                if(pos==2&&cnt>0)flag=1;
                pos++;
            }while(pos<len-1);
            if(cnt==0&&flag&&str[pos]=='~')ans=0;
        }
        else if(str[0]=='>'&&str[1]=='^'){
            int pos=2;
            int flag=1;
            do{
                if(str[pos]!='Q'||str[pos+1]!='='){
                    flag=0;
                    break;
                }
                pos+=2;
            }while(pos<len-2);
            if(flag&&str[pos]=='~'&&str[pos+1]=='~')ans=1;
        }
        if(ans==0)cout<<"A"<<endl;
        else if(ans==1)cout<<"B"<<endl;
        else cout<<"NA"<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1200
final_rank: 3

code_snippet: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int solve(string &s){
  if(s.length() >= 6 && s.substr(0, 2) == ">'" && s[(int)s.length() - 1] == '~'){
    int c1 = 0, c2 = 0, t = 0, f = 0;
    for(int i = 2; i + 1 < (int)s.length(); ++i){
      if(s[i] != '=' && s[i] != '#') f = 1;
      if(s[i] == '#' && t >= 1) f = 1;
      if(f) break;
      if(s[i] == '#') ++t;
      else if(t) ++c2;
      else ++c1;
    }
    if(!f && c1 == c2) return 0;
  }
  if(s.length() >= 6 && s.substr(0, 2) == ">^" && s.substr((int)s.length() - 2) == "~~"){
    int f = 0;
    for(int i = 2; i + 2 < s.length(); i += 2) if(s.substr(i, 2) != "Q=") f = 1;
    if(!f) return 1;
  }
  return -1;
}

int main() {
  int T; cin >>T;
  REP(t, T){
    string s; cin >>s;
    int ans = solve(s);
    cout <<(ans == 0 ? "A" : (ans == 1 ? "B" : "NA")) <<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 12

code_snippet: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <tuple>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};

//other
//--------------------------------------------
#define RANGE(a, c, b) ((a <= b) && (b < c))

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int solve(string snake){
	if(snake[0] != '>') return 0;
	if(snake[1] == '\''){
		int i = 2;
		while(snake[i] == '='){
			i++;
		}
		if(snake[i] != '#') return 0;
		int length = i - 2;
		i++;
		while(snake[i] == '='){
			i++;
		}
		int length2 = i -3 - length;
		if(length2 != length) return 0;
		if(length > 0 &&
			snake[i] == '~' && 
			i == snake.length() - 1){
			return 1;
		}
	}else if(snake[1] == '^'){
		int i = 2;
		int length = 0;
		while(snake[i] =='Q' &&
			snake[i + 1] == '='){
			i += 2;
			length++;
		}
		if(length > 0 &&
			snake[i] == '~' &&
			snake[i + 1] == '~' &&
			i + 1 == snake.length() - 1){
			return 2;
		}
	}
	return 0;
}

int main(int argc, char const *argv[])
{
	int n;
	cin >> n;
	REP(i, n){
		string snake;
		cin >> snake;
		string ans;
		switch(solve(snake)){
			case 0:
			ans = "NA";
			break;

			case 1:
			ans = "A";
			break;

			case 2:
			ans = "B";
			break;
		}
		cout << ans << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

bool check1(string s) {
	int n = s.size();

	if (n < 6 || s[0] != '>' || s[1] != '\'' || s[n - 1] != '~') {
		return false;
	}

	string sub = s.substr(2, n - 3);
	n = sub.size();

	if (!(n % 2) || sub[n / 2] != '#') {
		return false;
	}

	for (int i = 0; i < n / 2; i++) {
		if (sub[i] != '=' || sub[n - i - 1] != '=') {
			return false;
		}
	}

	return true;
}

bool check2(string s) {
	int n = s.size();

	if (n < 6 || s[0] != '>' || s[1] != '^' || s[n - 1] != '~' || s[n - 2] != '~') {
		return false;
	}

	string sub = s.substr(2, n - 4);
	n = sub.size();

	for (int i = 0; i < n / 2; i++) {
		if (sub[i * 2] != 'Q' || sub[i * 2 + 1] != '=') {
			return false;
		}
	}

	return true;
}

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

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

		if (check1(s)) {
			cout << "A" << endl;
		}else if (check2(s)) {
			cout << "B" << endl;
		}else {
			cout << "NA" << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 6

code_snippet: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());
int main(){
	int n;
	cin>>n;
	rep(i,n){
		string s;
		cin>>s;
		if(s.substr(0,2)==">'"&&s.substr(s.size()-1,1)=="~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-1);
			bool f=true;
			if(s.size()==0) f=false;
			if(s.size()&&s[0]!='='){
				f=false;
			}
			while(s.size()>1){
				if(s[0]==s[s.size()-1]&&s[0]=='='){
					s=s.substr(1,s.size()-2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				if(s[0]=='#'){
					cout<<"A\n";
				}
				else cout<<"NA\n";
			}
			else{
				cout<<"NA\n";
			}
		}
		else if(s.substr(0,2)==">^"&&s.substr(s.size()-2,2)=="~~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-2);
			bool f=true;
			if(s.size()<2) f=false;
			if(s.size()>=2&&s.substr(0,2)!="Q=") f=false;
			while(s.size()>=2){
				if(s.substr(0,2)=="Q="){
					s=s.substr(2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				cout<<"B\n";
			}
			else cout<<"NA\n";
		}
		else{
			cout<<"NA\n";
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 12

code_snippet: #include <iostream>
#include <string>

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

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	rep(i,n)
	{
		string s;
		cin >> s;

		if(s[0] == '>' && s[1] == '\'' && s[2] == '=')
		{
			int i = 2;
			int cnt = 0;
			while(s[i] == '=')
			{
				i++;
				cnt++;
			}

			if(s[i] == '#' && i < s.size())
			{
				i++;
				while(s[i] == '=')
				{
					if(i == s.size()-1) break;
					i++;
					cnt--;
				}
			}

			if(cnt == 0 && s[i] == '~') cout << "A" << endl;
			else cout << "NA" << endl;
		}
		else if(s[0] == '>' && s[1] == '^' && s[2] == 'Q')
		{
			int i = 2;
			int cnt = 0;
			bool end = false;
			while(i+1 < s.size())
			{
				if(s[i] == 'Q' && s[i+1] == '=')
				{
					i += 2;
				}
				else if(s[i] == '~' && s[i+1] == '~')
				{
					end = true;
					cout << "B" << endl;
					i += 2;
				}
				else
				{
					break;
				}
			}

			if (!end) cout << "NA" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1204
final_rank: 5