#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int n;
string s;
bool used[12];
long long c[12];
int main() {
c[0] = 0;
c[1] = 1;
for(int i=2;i<12;++i) {
c[i]=c[i-1]*i;
}
cin>>n;
for(int i=0;i<n;++i) {
cin>>s;
memset(used,0,sizeof(used));
long long ans=0;
for(int j=0;j<12;++j) {
int p = s[j]-'a';
int cnt = 0;
for(int k=p-1;k>=0;--k) {
if(!used[k])
++cnt;
}
used[p]=1;
ans += cnt*c[11-j];
}
ans+=1;
printf("%lld\n",ans);
}
return 0;
}