#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
#define sz(x) (ll)(x).size()
#define rd ({ll x; cin >> x; x; })
#define dbg(x) cerr << "[" #x "] " << (x) << "\n"
// #define errv(x) {cerr << "["#x"] ["; for (const auto& ___ : (x)) cerr << ___ << ", "; cerr << "]\n";}
// #define errvn(x, n) {cerr << "["#x"] ["; for (auto ___ = 0; ___ < (n); ++___) cerr << (x)[___] << ", "; cerr << "]\n";}
// #define cerr if(0)cerr
#define xx first
#define yy second
mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
/*_________________________________________________________________________________________________________________________*/
ll calc(ll mask, vector<vector<ll>>& need, ll i, ll n, vector<vector<ll>>& dp)
{
if (i == n) {
if (mask == 0) {
return 0;
} else {
return INT_MAX;
}
}
if (dp[i][mask] != -1) {
return dp[i][mask];
}
dp[i][mask] = calc(mask, need, i + 1, n, dp);
for (int j = 0; j < 9; j++) {
if (mask & (1 << j)) {
dp[i][mask] = min(dp[i][mask] , need[i][j] + calc(mask ^ (1 << j), need, i + 1, n, dp));
}
}
return dp[i][mask];
}
void Solve()
{
string a;
cin >> a;
ll n = sz(a);
string base = "SERIOUSOJ";
vector<vector<ll>> need(n, vector<ll>(9, 0)), nextNeed;
vector<bool> weWillTake(n, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 9; j++) {
if (j == 6) {
need[i][j] = need[i][0];
continue;
} else if (j == 7) {
need[i][j] = need[i][4];
continue;
}
char target = base[j];
ll l2r, r2l;
if (target >= a[i]) {
l2r = target - a[i];
} else {
l2r = 26 - (a[i] - target);
}
if (target <= a[i]) {
r2l = a[i] - target;
} else {
r2l = 26 - (target - a[i]);
}
need[i][j] = min(l2r, r2l);
}
}
for (int i = 0; i < 9; i++) {
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
for (int j = 0; j < n; j++) {
pq.push({ need[j][i], j });
}
for (int j = 0; j < 9; j++) {
weWillTake[pq.top().yy] = 1;
pq.pop();
}
}
for (int i = 0; i < n; i++) {
if (weWillTake[i]) {
nextNeed.push_back(need[i]);
}
}
n = sz(nextNeed);
vector<vector<ll>> dp(n + 1, vector<ll>(1 << 9, -1));
dp[0][0] = 0;
ll mask = (1ll << 9) - 1;
ll ans = calc(mask, nextNeed, 0, n, dp);
cout << ans << '\n';
}
int32_t main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": "; // cout << "Case " << i << ": ";
Solve();
}
return 0;
}
// Coded by Tahsin Arafat (@TahsinArafat)