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

int N;
int main()
{
  cin >> N;
  for(int i = 0; i < N; i++)
  {
    string s; 
    cin >> s;
    int p = 0;
    try
    {
      if(s[p++] != '>')
        goto NA;
      int m = 0;
      switch(s[p])
      {
        case '\'':
          while(s[++p] == '=')
            m++;
          if(s[p++] != '#')
            goto NA;
          for(int j = 0; j < m; j++, p++)
            if(s[p] != '=')
              goto NA;
          if(s[++p] != '~')
            goto NA;
          if(p != s.length())
            goto NA;
          cout << "A" << endl;
          continue;
        case '^':
          p++;
          for(; p < s.length() - 2; p++)
          {
            if(p % 2 == 0 && s[p] != 'Q' || p % 2 == 1 && s[p] != '=')
              goto NA;
          }
          if(s[p++] != '~' && s[p] != '~')
            goto NA;
          cout << "B" << endl;
          continue;
      }
    }
    catch(string str)
    {
      cout << "NA" << endl;
      continue;
    }
    continue;
NA:
    cout << "NA" << endl;
    continue;
  }
}