package com.thinker.bishi.saima;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author lzh
 */
public class Test2 {
    private LinkedList<Character> base = new LinkedList<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] strings = new String[n];
        for (int i = 0; i < n; i++){
            strings[i] = in.next();
        }

        for(int i=0;i < n;i++) {
            Test2 mainn = new Test2("abcdefghijkl");
            System.out.println(mainn.rankString(strings[i]));
        }

    }
    public Test2(String s){
        char[] chars = s.toCharArray();
        int n = chars.length;
        for(int i = 0; i < n ; i++){
            base.add(chars[i]);
        }
    }
    public int rankString(String target){
        int rank = 1;
        int size = base.size();
        for(int i = 0; i<size;i++){
            int locOfIthChar =base.indexOf(target.charAt(i));
            base.remove(locOfIthChar);
            rank += locOfIthChar*factorial(size-i-1);
        }
        return rank;
    }
    private int factorial(int n){
        int ans = 1;
        for(int i = 1 ; i<=n;i++){
            ans*= i;
        }
        return ans;
    }
}
