#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();
// For each target letter, keep the best few source letters
vector<vector<pair<int, int>>> cand(K); // (cost, pos)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < K; ++j) {
int c = shiftCost(S[i], target[j]);
cand[j].emplace_back(c, i);
}
}
for (int j = 0; j < K; ++j) {
sort(cand[j].begin(), cand[j].end());
if (cand[j].size() > 20) cand[j].resize(20);
}
// Collect unique source indices
vector<int> nodes;
for (int j = 0; j < K; ++j) {
for (auto [c, pos] : cand[j]) {
nodes.push_back(pos);
}
}
sort(nodes.begin(), nodes.end());
nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());
int M = nodes.size();
// Build cost matrix for KM
vector<vector<int>> cost(K, vector<int>(M, INF));
for (int j = 0; j < K; ++j) {
for (auto [c, pos] : cand[j]) {
int idx = lower_bound(nodes.begin(), nodes.end(), pos) - nodes.begin();
cost[j][idx] = c;
}
}
// KM Hungarian: find min-cost perfect matching
// Template for small K
vector<int> u(K + 1), v(M + 1), p(M + 1), way(M + 1);
for (int i = 1; i <= K; ++i) {
p[0] = i;
vector<int> minv(M + 1, INF);
vector<bool> used(M + 1, false);
int j0 = 0;
do {
used[j0] = true;
int i0 = p[j0], delta = INF, j1;
for (int j = 1; j <= M; ++j) {
if (!used[j]) {
int cur = cost[i0 - 1][j - 1] - u[i0] - v[j];
if (cur < minv[j]) {
minv[j] = cur;
way[j] = j0;
}
if (minv[j] < delta) {
delta = minv[j];
j1 = j;
}
}
}
for (int j = 0; j <= M; ++j) {
if (used[j]) {
u[p[j]] += delta;
v[j] -= delta;
} else {
minv[j] -= delta;
}
}
j0 = j1;
} while (p[j0] != 0);
do {
int j1 = way[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0);
}
int ans = -v[0];
cout << ans << '\n';
}
}