#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	string base = "atcoder";
	while(t--){
		string s;
		cin >> s;
		int at = 0, n = s.size();
		while(at < base.size() && at < n && base[at] == s[at]) at++;
		if((at < base.size() && at < n && base[at] < s[at]) || (at == base.size() && at < n)) cout << 0 << '\n';
		else if(at == 1){
			int ans = 1<<30;
			while(at < n && s[at] == 'a') at++;
			if(at == n) ans = -1;
			else {
				if(s[at] > 't') ans = at - 1;
				else ans = at;
			}
			cout << ans << '\n';
		} else {
			assert(at > 1);
			cout << 1 << '\n';
		}
	}
	return 0;
}

#include <iostream>
#include <string>

int main() {
  const std::string atcoder = "atcoder";
  int T;
  std::cin >> T;
  for (int t = 0; t < T; ++t) {
    std::string S;
    std::cin >> S;
    if (S > atcoder) {
      std::cout << 0 << std::endl;
      continue;
    }
    int index = -1;
    for (std::size_t i = 0; i < S.size(); ++i) {
      if (S[i] > 'a') {
        index = i;
        break;
      }
    }
    if (index <= 0) {
      std::cout << index << std::endl;
      continue;
    }
    for (int count = 1, i = index - 1; i >= 0; --i, ++count) {
      std::swap(S[i], S[i + 1]);
      if (S > atcoder) {
        std::cout << count << std::endl;
        break;
      }
    }
  }
  return 0;
}
