Under construction

Still being built. Things may change

Cyclic Shifts

Time limit2sMemory limit512 MB

Summary
For each prefix of a string, find the smallest index of its lexicographically minimal cyclic shift, then sum these indices weighted by powers of 8753 modulo 1000000123.
Level

Hard8 of 10

Topics
String, String matching, Math
Solved
No attempts yet

Statement

Let s=s1s2…sks = s_1 s_2 \ldots s_k be a string of length kk. For any integer ii between 0 and k−1k-1, inclusive, the ii-th cyclic shift of ss is the string si+1si+2…sks1…sis_{i+1}s_{i+2} \ldots s_k s_1 \ldots s_i. For example, the 4-th cyclic shift of wellplayed is playedwell, and the 0-th cyclic shift of metro is metro itself.

For a string of length kk, f(s)f(s) is the index ii such that the ii-th cyclic shift of ss is the lexicographically smallest among all its cyclic shifts. If several such ii exist, f(s)f(s) is the smallest of them. For example, f(acabbac) = 2 and f(cabcab) = 1.

For a string of length nn, g(s)g(s) is the sum of f(s1s2…sk)⋅8753k−1f(s_1 s_2 \ldots s_k) \cdot 8753^{k-1} over all kk from 1 to nn, inclusive. For each given string ss, find the value of g(s)g(s) modulo 109+12310^9 + 123.

Input

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4), the number of test cases. Each of the next tt lines contains a non-empty string of lowercase English letters. The total length of the input strings does not exceed 10610^6.

Output

For each input string, in input order, output a single integer: g(s)g(s) modulo 109+12310^9 + 123.

Examples1

  1. Example 1

    Input
    2
    aab
    acabbac
    
    Expected output
    0
    38098220