Cham was studying Permutation and Combinations . He was deeply fascinated by the problem that asks to calculate the rank of the word S in a dictionary (given that the dictionary only comprises of the alphabets that are present in S and all the words in the dictionary have the same length as S and same number of alphabets as in S )
For the calculation procedure please refer to the given link :- Rank of a Word
You are provided with a list of n words . Your task is simple help Cham to calculate the Rank of words .
Input Format:
First line contains the integer n , denoting the number of words in the list. Next n lines contain the word S , whose rank is to be calculated.
Output Format:
For each word in the list , print the rank of the corresponding word in new line.
Constraints:
1 <= n <= 100
1 <= length(S) <= 15
MOTHER : deeply explained in the provided link
COW : arranging in lexicographical order we have C,O,W . Therefore , the first word in the dictionary will itself be COW. Hence, its rank is 1
RANK : following the steps as in the link we have rank of "RANK" = 3! + 3! + 3! + 1 + 1 = 20
NITISH : check the comment section in the given link