Cyclic Shifts

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Let s=s_1s_2s_ks = s\_1 s\_2 \ldots s\_k be a string of length kk. For any integer ii between 0 and k1k-1, inclusive, we define the ii-th cyclic shift of ss as the string s_i+1s_i+2s_ks_1s_is\_{i+1}s\_{i+2} \ldots s\_k s\_1 \ldots s\_i. For example, the 4-th cyclic shift of "wellplayed" is "playedwell", while the 0-th cyclic shift of "metro" is "metro" itself.

Let's define a function f(s)f(s) which, for a string of length kk, is equal to ii such that the ii-th cyclic shift of ss is the lexicographically smallest among all its cyclic shifts. If there are several such ii's, then f(s)f(s) is equal to the smallest of them. For example, f(f("acabbac")=2) = 2, while f(f("cabcab")=1) = 1.

Let's define a function g(s)g(s) which, for a string of length nn, is equal to the sum of f(s_1s_2...s_k)8753k1f(s\_1 s\_2...s\_k) \cdot 8753^{k-1} over all kk between 11 and nn, inclusive.

For each given string ss, find the value of g(s)g(s) modulo 109+12310^9 + 123.

입력

The first line of the input contains a single integer tt (1t1041 \le t \le 10^4) --- the number of test cases.

Each of the next tt lines contains a non-empty string consisting of lowercase English letters.

The total length of the input strings doesn't exceed 10610^6.

출력

For each string ss in order of input, output a single integer --- the value of g(s)g(s) modulo 109+12310^9 + 123.

힌트

In the first example test case, f(f("a")=0) = 0, f(f("aa")=0) = 0, and f(f("aab")=0) = 0. Therefore, g(g("aab")=0) = 0.

Here is the list of values of f(s_1s_2s_k)f(s\_1s\_2 \ldots s\_k) for the second example test case:

  • f(f("a")=0) = 0;
  • f(f("ac")=0) = 0;
  • f(f("aca")=2) = 2;
  • f(f("acab")=2) = 2;
  • f(f("acabb")=2) = 2;
  • f(f("acabba")=5) = 5;
  • f(f("acabbac")=2) = 2.

Thus, g(g("acabbac")=287532+287533+287534+587535+287536=899695598935764095704157) = 2 \cdot 8753^2 + 2 \cdot 8753^3 + 2 \cdot 8753^4 + 5 \cdot 8753^5 + 2 \cdot 8753^6 = 899695598935764095704157, which is equal to 3809822038098220 modulo 109+12310^9 + 123.