#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
map<char, int> mp = {{'I', 1}, {'E', 4}, {'S', 1}, {'B', 1}, {'C', 1}, {'H', 1}, {'A', 1}, {'P', 1}, {'T', 1}, {'R', 1}};
void solve() {
string s1;
cin >> s1;
int n = s1.size(), ans = -1;
s1 = '#' + s1; // 1'base
vector<vector<int>> pre(26, vector<int> (n + 1, 0));
for(int i = 1; i <= n; i++) {
pre[s1[i] - 'A'][i]++;
for(int ch = 0; ch < 26; ch++) pre[ch][i] += pre[ch][i - 1];
}
int lo = 1, hi = n, mid;
while(lo <= hi) {
mid = lo + hi >> 1;
auto isPossible = [&](int k) -> bool {
int l = 1, r = k;
while(r <= n) {
bool ok = 1;
for(auto &[ch, cnt]: mp) {
if(cnt > pre[ch - 'A'][r] - pre[ch - 'A'][l - 1]) {
ok = 0;
break;
}
}
if(ok) return 1;
++l, ++r;
}
return 0;
};
if(isPossible(mid)) {
ans = mid;
hi = mid - 1;
}
else lo = mid + 1;
}
cout << ans << endl;
return;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case " << t << ": ";
solve();
}
return 0;
}