#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int minShiftCost(char from, char to) {
int d = abs(from - to);
return min(d, 26 - d);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
string target = "SERIOUSOJ";
while (T--) {
string S;
cin >> S;
int n = S.size();
vector<int> dp(1 << 9, INF);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
vector<int> ndp = dp; // copy previous dp
for (int mask = 0; mask < (1 << 9); ++mask) {
if(i + 1 < __builtin_popcount(mask))
continue;
for (int j = 0; j < 9; ++j) {
if ((mask & (1 << j)) == 0) {
int newMask = mask | (1 << j);
int cost = minShiftCost(S[i], target[j]);
ndp[newMask] = min(ndp[newMask], dp[mask] + cost);
}
}
}
dp = move(ndp);
}
cout << dp[(1 << 9) - 1] << '\n';
}
return 0;
}