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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using LD = long double;

template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> void coutall(T v) { for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; }
void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }
ll myceil(ll a, ll b) { return (a + (b - 1)) / b; }

void Main(){
    int n; cin >> n;
    n = (1<<n);
    cout << n-1 << endl;
    rep3(i, 1, n-1){
        rep(j, n){
            int tmp = __builtin_popcount(i&j);
            cout << (tmp&1 ? 'B' : 'A');
        }
        cout << endl;
    }
    return;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int Maxx=1000001,Maxn=2001,INF=0x3f3f3f3f,Mod=1e9+7;
inline int read()
{
	int res=0,bj=0;char ch=getchar();
	while(ch<'0'||ch>'9')bj|=(ch=='-'),ch=getchar();
	while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
	return bj?-res:res;
}
void print(int x)
{
	if(x>9)print(x/10);
	putchar(x%10^48);
}
inline void printnum(int x,char ch)
{
	if(x<0)putchar('-'),x=-x;
	print(x),putchar(ch);
}
char S[1005],P[10]="atcoder";
int main()
{
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	int T=read()+1,n;
	while(--T){
		scanf("%s",S),n=strlen(S);
		int ans=1e9,p=0;
		for(int i=0;i<8;++i)
		if(S[i]!=P[i]){
			if(S[i]>P[i])ans=0;
			i=8;
		}
		for(int i=0,x;i<min(7,n);++i){
			x=n;
			for(int j=i;j<n;++j)
			if(S[j]>P[i])x=j,j=n;
			if(x<n)ans=min(ans,p+x-i);
			x=n;
			for(int j=i;j<n;++j)
			if(S[j]>=P[i])x=j,j=n;
			if(x==n)break;
			p+=x-i;for(int j=x;j>i;--j)swap(S[j],S[j-1]);
		}
		cout<<(ans==1e9?-1:ans)<<"\n";
	}
	return 0;
}