http://acm.nyist.net/JudgeOnline/problem.php?pid=139
/*
用的是康托展开
http://zh.wikipedia.org/zh/康托展开
*/
#include<stdio.h>
int s[13]={1, 1, 2};
int pre_count( int i, char *a )
{
int count = 0;
//if( i == 0 ) return 0;
for( int j = 0; j < i; j ++ )
if( a[j] < a[i] ) ++ count;
return count;
}
void f()
{
for( int i = 3; i < 12; i ++ )
s[i] = s[i-1]*i;
}
int main()
{
f();
int t, n, i, sor;
char str[13];
scanf( "%d", &t );
while( t -- )
{
int pre;
sor = pre = 0;
scanf( "%s", str );
for( i = 0; i < 12; i ++ )
{
pre = pre_count( i, str );
sor +=( str[i]-'a'-pre)*s[11-i];
}
printf( "%d\n", sor+1 );
}
}