/*
*Copyright (c) Swadheen Islam Robi (SIR01)
*Created on Sun Aug 31 2025 3:23:15 AM
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pie 2 * (acos(0.0))
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define pb push_back
#define endl "\n"
#define lcm(a, b) (a * b) / (__gcd<ll>(a, b))
#define mod 1000000007
#define srt(v) sort(v.begin(), v.end())
#define rsrt(v) sort(v.rbegin(), v.rend())
#define ALLAHU_AKBAR \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
char a[12] = {'I', 'E', 'L', 'U', 'S', 'B', 'C', 'H', 'A', 'P', 'T', 'R'};
map<char, ll> chk;
void sir()
{
string s;
cin >> s;
map<char, ll> mp;
for (auto c : s)
mp[c]++;
for (ll i = 0; i < 12; i++)
{
char c = a[i];
if (c == 'E' && mp[c] < 4 || (!mp[c]))
{
cout << -1 << endl;
return;
}
}
// two pointer
ll i = 0, j = s.length() - 1;
ll one = 0, two = 0;
while (i <= j)
{
char c = s[i], c2 = s[j];
if (chk[c] > 0)
{
if (chk[c] > 0 && mp[c] > chk[c])
{
i++;
mp[c]--;
}
else
one = 1;
}
else
i++;
if (chk[c2] > 0)
{
if (mp[c2] > chk[c2])
{
j--;
mp[c2]--;
}
else
two = 1;
}
else
j--;
if (one && two)
break;
}
cout << j - i + 1 << endl;
}
int main()
{
ALLAHU_AKBAR
for (ll i = 0; i < 12; i++)
{
if (a[i] == 'E')
chk[a[i]] = 4;
else
chk[a[i]] = 1;
}
ll t = 1;
cin >> t;
while (t--)
{
sir();
}
return 0;
}