#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int shiftCost(char a, char b) {
int d = abs(a - b);
return min(d, 26 - d);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
string target = "SERIOUSOJ";
int K = 9;
while (T--) {
string S;
cin >> S;
int n = S.size();
vector<vector<pair<int, int>>> costs(K); // (cost, pos)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < K; ++j) {
int c = shiftCost(S[i], target[j]);
costs[j].emplace_back(c, i);
}
}
// For each target letter, keep only best 20 source letters
for (int j = 0; j < K; ++j) {
sort(costs[j].begin(), costs[j].end());
if (costs[j].size() > 20) costs[j].resize(20);
}
int ans = INF;
vector<int> perm(K);
iota(perm.begin(), perm.end(), 0);
do {
// Try this target letter order
int total = 0;
set<int> used_pos;
bool valid = true;
for (int j = 0; j < K; ++j) {
bool found = false;
for (auto [c, pos] : costs[perm[j]]) {
if (used_pos.count(pos)) continue;
total += c;
used_pos.insert(pos);
found = true;
break;
}
if (!found) {
valid = false;
break;
}
}
if (valid) ans = min(ans, total);
} while (next_permutation(perm.begin(), perm.end()));
cout << ans << '\n';
}
}