Counting Distinct Subsequences

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

Medium6Dynamic programmingStringMathNo attempts yetTime limit1sMemory limit512 MB

Problem

Let S=S1S2SNS = S_1S_2\cdots S_N be a string. Every string of the form Si1Si2SikS_{i_1}S_{i_2}\cdots S_{i_k} with 0kN0 \le k \le N and 1i1<i2<<ikN1 \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 (1T100001 \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}.