#include using namespace std; void solve(const string& S) { int N = S.size(); vector min_right(N + 1, 'z' + 1); for (int i = N - 1; i >= 0; --i) { min_right[i] = min(S[i], min_right[i + 1]); } int count = 0; for (int i = 0; i < N - 1; ++i) { if (S[i] < min_right[i + 1]) { ++count; } } cout << count << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { string S; cin >> S; solve(S); } return 0; }