#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
//#include <queue>
//#include <numeric>
#include <cassert>
using namespace std;
#ifdef LOCAL_DEBUG
#include <local_debug.h>
#define DEBUG(...) DBG2::cprint(#__VA_ARGS__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif
#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
using llong = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using II = pair<int,int>;
const int INF = 1e9 + 123;
const string SERIOUS = "SERIOUSOJ";
string T;
VVI memo;
int go(int n, int mask) {
if (mask == (1<<SZ(SERIOUS))-1) {
return 0;
}
if (n >= SZ(T))
return INF;
int& res = memo[n][mask];
if (res < 0) {
res = go(n+1, mask);
REP(j, SZ(SERIOUS)) {
if (0 == (mask & (1<<j))) {
int dist = abs(SERIOUS[j] - T[n]);
if (dist > 26 - dist)
dist = 26 - dist;
int cur = dist + go(n + 1, mask | (1<<j));
res = min(res, cur);
}
}
}
return res;
}
int solve(const string& S) {
VI freq(26);
for (char c : S)
freq[c-'A']++;
T.clear();
REP(k, 26)
T += string(min(freq[k], 9), 'A'+k);
DEBUG(T);
memo = VVI(T.size(), VI(1<<SZ(SERIOUS), -1));
int res = go(0, 0);
return res;
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC;
cin >> TC;
FOR(tc, 1, TC) {
string S;
cin >> S;
int res = solve(S);
cout << res << '\n';
}
return 0;
}