#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<string> ans{"AB"};
  FOR(i, 2, n + 1) {
    int per = 1 << i, m = ans.size();
    vector<string> nx{""};
    REP(_, per / 2) nx.back() += 'A';
    REP(_, per / 2) nx.back() += 'B';
    for (auto &s : ans) {
      nx.emplace_back(s + s);
      string t = s;
      for (char c : s) t += c == 'A' ? 'B' : 'A';
      nx.emplace_back(t);
    }
    ans.swap(nx);
  }
  int k = ans.size();
  assert(k == (1 << n) - 1);
  // vector a(1 << n, vector(1 << n, 0)), b(1 << n, vector(1 << n, 0));
  // for (auto &s : ans) {
  //   REP(i, 1 << n) REP(j, 1 << n) {
  //     if (j != i) (s[i] == s[j] ? a[i][j] : b[i][j]) += 1;
  //   }
  // }
  // int N = a[0][1], M = b[0][1];
  // REP(i, n) FOR(j, i + 1, n) assert(a[i][j] == N && b[i][j] == M);
  cout << k << '\n';
  for (auto &s : ans) cout << s << '\n';
  return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve(){
	string s; cin >> s;
	if (s > "atcoder"){
		cout << 0 << "\n"; return;
	}
	ll n = s.length();
	for (ll i = 0; i < n; i++){
		if (s[i] == 'a') continue;
		else{
			if (i == 1){
				cout << 1 << "\n"; return;
			}
			else{
				if (s[i] > 't'){
					cout << i - 1 << "\n"; 
				} 
				else cout << i << "\n";
				return;
			}
		}
	}
	cout << -1 << "\n"; return;
}
int main(){
	fastio;
	ll t; cin >> t;
	while (t--) solve();
}