using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 3");
WillReturn.Add("ABCBA");
WillReturn.Add("CCBAB");
WillReturn.Add("AAAAA");
//3
//2
//0
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 5");
WillReturn.Add("AC");
WillReturn.Add("AC");
WillReturn.Add("AC");
WillReturn.Add("AC");
WillReturn.Add("AC");
//0
//0
//0
//0
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("13 1");
WillReturn.Add("ABCCABCBACBAA");
//9
}
else if (InputPattern == "Input4") {
WillReturn.Add("13 4");
WillReturn.Add("CCAAACBAAAABB");
WillReturn.Add("BBBCCBCCCBCBC");
WillReturn.Add("CCCAAAABBBBBB");
WillReturn.Add("AABCBCACBACBA");
//4
//6
//2
//10
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
// 10べき[指数]
static Dictionary<long, long> mBeki10Dict = new Dictionary<long, long>();
// 最上位の10べきを返す
static long DeriveTopBeki10(long pVal)
{
for (long I = 17; 0 <= I; I--) {
if (pVal >= mBeki10Dict[I]) {
return mBeki10Dict[I];
}
}
return 1;
}
static void Main()
{
long Beki10 = 1;
for (long I = 0; I <= 17; I++) {
mBeki10Dict[I] = Beki10;
Beki10 *= 10;
}
List<string> InputList = GetInputList();
var StrSet = new HashSet<string>();
StrSet.UnionWith(InputList.Skip(1));
var GoodStrSet = new HashSet<string>();
foreach (string EachStr in StrSet) {
string GoodStr = DeriveGoodStr(EachStr);
GoodStrSet.Add(GoodStr);
}
// 多始点BFS
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
foreach (string EachStr in GoodStrSet) {
WillEnqueue.CurrVal = GetNum(EachStr);
WillEnqueue.Level = 0;
Que.Enqueue(WillEnqueue);
long GoodNum = GetNum(EachStr);
mMemo[GoodNum] = 0;
}
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
long TopBeki10 = DeriveTopBeki10(Dequeued.CurrVal);
for (long I = TopBeki10 / 10; 1 <= I; I /= 10) {
long NewVal = ExecShift(Dequeued.CurrVal, TopBeki10, I);
WillEnqueue.CurrVal = NewVal;
WillEnqueue.Level = Dequeued.Level + 1;
if (mMemo.ContainsKey(WillEnqueue.CurrVal)) {
if (mMemo[WillEnqueue.CurrVal] <= WillEnqueue.Level) {
continue;
}
}
mMemo[WillEnqueue.CurrVal] = WillEnqueue.Level;
Que.Enqueue(WillEnqueue);
}
}
var sb = new System.Text.StringBuilder();
foreach (string EachStr in InputList.Skip(1)) {
long Result = mMemo[GetNum(EachStr)];
sb.Append(Result);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
static Dictionary<long, long> mMemo = new Dictionary<long, long>();
struct JyoutaiDef
{
internal long CurrVal;
internal long Level;
}
// 文字列を引数とし、良いパンケーキタワーな文字列を返す
static string DeriveGoodStr(string pStr)
{
char[] CharArr = pStr.ToCharArray();
int ACnt = CharArr.Count(pX => pX == 'A');
int BCnt = CharArr.Count(pX => pX == 'B');
int CCnt = CharArr.Count(pX => pX == 'C');
var sb = new System.Text.StringBuilder();
sb.Append(new string('A', ACnt));
sb.Append(new string('B', BCnt));
sb.Append(new string('C', CCnt));
return sb.ToString();
}
// Aを1、Bを2、Cを3に変換した数値を返す
static long GetNum(string pStr)
{
long WillReturn = 0;
long Omomi = 1;
for (int I = pStr.Length - 1; 0 <= I; I--) {
if (pStr[I] == 'A') WillReturn += Omomi * 1;
if (pStr[I] == 'B') WillReturn += Omomi * 2;
if (pStr[I] == 'C') WillReturn += Omomi * 3;
Omomi *= 10;
}
return WillReturn;
}
// long型で、先頭桁の回転を行う
static long ExecShift(long pTargetVal, long pStaShisuu, long pEndShisuu)
{
while (pStaShisuu > pEndShisuu) {
long Num1 = (pTargetVal / pStaShisuu) % 10;
long Num2 = (pTargetVal / pEndShisuu) % 10;
pTargetVal += (Num2 - Num1) * pStaShisuu;
pTargetVal += (Num1 - Num2) * pEndShisuu;
pStaShisuu /= 10;
pEndShisuu *= 10;
}
return pTargetVal;
}
}