#include typedef long long ll; #define mod 1000000007 #define all(x) x.begin(),x.end() ll fpow(ll n, ll k, ll p = mod) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} ll inv(int a, ll p = mod) {return fpow(a, p - 2, p);} using namespace std; ll chk[26]; //hash array ll dp1[26][100003],dp2[26][100003]; ll n; string s; void init1() { memset(chk,0,sizeof(chk)); for(ll i=0;s[i]!='\0';i++) chk[s[i]-'a']++; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t;cin>>t; while(t--) { cin>>s; n=s.length(); init1(); //hashes the characters of string s in a frequency array ll ini_cnt=0; //ini_cnt stores the number of pairs (i,j) such that i=0;i--) { ll temp=s[i]-'a'; for(ll j=0;ji) dp2[i][n-1]=1; for(ll j=n-2;j>=0;j--) { ll temp=s[j]-'a'; if(temp>i) dp2[i][j]=dp2[i][j+1]+1; else dp2[i][j]=dp2[i][j+1]; } } //ans stores our final answer ll ans=LLONG_MAX; for(ll i=0;i