Under construction

Still being built. Things may change

Counting Distinct Subsequences

Time limit1sMemory limit512 MB

Summary
Count the distinct subsequences of a string, including the empty string, for up to 10,000 test cases.
Level

Medium6 of 10

Topics
Dynamic programming, String, Math
Solved
No attempts yet

Problem

Let S=S1S2⋯SNS = S_1S_2\cdots S_N be a string. Every string of the form Si1Si2⋯SikS_{i_1}S_{i_2}\cdots S_{i_k} with 0≤k≤N0 \le k \le N and 1≤i1<i2<⋯<ik≤N1 \le i_1 < i_2 < \cdots < i_k \le N is called a subsequence of SS. The empty string, of length 0, is also a subsequence of SS. For example, the string ioi has 7 distinct subsequences: the empty string, i, o, ii, io, oi, and ioi.

Given a string SS, find the number of distinct subsequences of SS.

Input

The first line contains the number of test cases TT (1≤T≤10 0001 \le T \le 10\,000). Each of the next TT lines contains one string SS. SS consists only of uppercase English letters, lowercase English letters, and the digits 0 to 9, and its length is between 1 and 1,000 inclusive. Uppercase and lowercase letters are different characters.

Output

For each test case, print the number of distinct subsequences of SS on its own line. The empty string is counted. Every input string is guaranteed to have an answer of at most 101810^{18}.

Examples1

  1. Example 1

    Input
    5
    ioi
    Mmmmm
    ERRATA
    0000FF
    R3GuLaM1N
    
    Expected output
    7
    10
    42
    15
    512