#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define all(v) v.begin(), v.end()
#define rep(i, begin, end) for(int i = begin; i < (int)(end); i++)
#define contains(v, x) (find(all(v), x) != v.end())
template<class T> bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> T roundup(T a, T b){ return (a + b - 1) / b; }
const double PI = 3.14159265359;
const vector<int> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const vector<int> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};
using ll = long long;
struct Edge{ int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} };
const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e9;
using P = pair<ll, int>;
using Graph = vector<vector<int>>;
/*#include <atcoder/all>
using namespace atcoder;
using mint = modint;*/

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    //mint::set_mod(MOD);
    
    string s;
    cin >> s;
    
    reverse(all(s));
    int n = s.size();
    ll ans = 0;
    vector<int> cnt(26);
    rep(i, 0, n - 1){
        if(s[i] == s[i + 1]){
            ans += i - cnt[s[i] - 'a'];
            fill(all(cnt), 0);
            cnt[s[i] - 'a'] = i + 2;
            i++;
        }else cnt[s[i] - 'a']++;
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
template<class T, class U> T &ctmax(T &x, const U &y){ return x = max<T>(x, y); }
template<class T, class U> T &ctmin(T &x, const U &y){ return x = min<T>(x, y); }
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T, class Compare = less<>> using Tree = tree<T, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;


// DEBUG BEGIN
#ifdef LOCAL
template<class L, class R> ostream &operator<<(ostream &out, const pair<L, R> &p){
	return out << "(" << p.first << ", " << p.second << ")";
}
template<class Tuple, size_t N> struct TuplePrinter{
	static ostream &print(ostream &out, const Tuple &t){ return TuplePrinter<Tuple, N-1>::print(out, t) << ", " << get<N-1>(t); }
};
template<class Tuple> struct TuplePrinter<Tuple, 1>{
	static ostream &print(ostream &out, const Tuple& t){ return out << get<0>(t); }
};
template<class... Args> ostream &print_tuple(ostream &out, const tuple<Args...> &t){
	return TuplePrinter<decltype(t), sizeof...(Args)>::print(out << "(", t) << ")";
}
template<class ...Args> ostream &operator<<(ostream &out, const tuple<Args...> &t){
	return print_tuple(out, t);
}
template<class T> ostream &operator<<(enable_if_t<!is_same<T, string>::value, ostream> &out, const T &arr){
	out << "{"; for(auto &x: arr) out << x << ", ";
	return out << (arr.size() ? "\b\b" : "") << "}";
}
template<size_t S> ostream &operator<<(ostream &out, const bitset<S> &b){
	for(int i = 0; i < S; ++ i) out << b[i];
	return out;
}
void debug_out(){ cerr << "\b\b " << endl; }
template<class Head, class... Tail>
void debug_out(Head H, Tail... T){ cerr << H << ", ", debug_out(T...); }
void debug2_out(){ cerr << "-----DEBUG END-----\n"; }
template<class Head, class... Tail>
void debug2_out(Head H, Tail... T){ cerr << "\n"; for(auto x: H) cerr << x << "\n"; debug2_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define debug2(...) cerr << "----DEBUG BEGIN----\n[" << #__VA_ARGS__ << "]:", debug2_out(__VA_ARGS__)
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// DEBUG END

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	auto __solve_tc = [&](int __tc_num){
		string s{"atcoder"}, t;
		cin >> t;
		int n = (int)t.size();
		int res = numeric_limits<int>::max();
		for(auto i = 0; i <= min(7, (int)t.size()); ++ i){ // first mismatch at i
			string obj(s.begin(), s.begin() + i);
			for(auto c = i != 7 ? s[i] + 1 : 'a'; c <= 'z'; ++ c){
				obj += c;
				vector<int> ord(n, -1);
				for(auto p = 0; p < (int)obj.size(); ++ p){
					for(auto i = 0; i < n; ++ i){
						if(!~ord[i] && obj[p] == t[i]){
							ord[i] = p;
							goto OK;
						}
					}
					goto DONE;
					OK:;
				}
				{
					int temp = (int)obj.size();
					Tree<int> tr;
					int cur = 0;
					for(auto &x: ord){
						if(!~x){
							x = temp ++;
						}
						cur += (int)tr.size() - tr.order_of_key(x);
						tr.insert(x);
					}
					debug(obj, cur);
					ctmin(res, cur);
				}
				DONE:;
				obj.pop_back();
			}
		}
		res == numeric_limits<int>::max() ? cout << "-1\n" : cout << res << "\n";
		return 0;
	};
	int __tc_cnt;
	cin >> __tc_cnt;
	for(auto __tc_num = 0; __tc_num < __tc_cnt; ++ __tc_num){
		__solve_tc(__tc_num);
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////