problems / 127

Smallest Repeating Block

easy strings string-matching 1000 ms · 256 MB

Smallest Repeating Block

You are given T strings. For each string s, find the length of the shortest non-empty string t such that s is equal to t written k times in a row, for some positive integer k.

Such a t always exists, because t = s with k = 1 works. So if s cannot be split into two or more identical copies of a shorter block, the answer is the length of s.

Input

The first line contains an integer T (1 ≤ T ≤ 10^5), the number of strings.

Each of the next T lines contains a string s of lowercase English letters with 1 ≤ |s|. The total length of all strings is at most 10^6.

Output

For each string, print the length of its shortest repeating block on its own line, in input order.

Notes

In the first example, abcabcabc is abc written three times, so the answer is 3. abab can be written as ab twice, so the answer is 2. abaab cannot be split into equal blocks: ab matches the start and the end, but the length 5 isn't a multiple of 2, so the answer is 5. zzzzzz is z written six times.

In the second example, aabaab is aab written twice. In abaaba, the shortest block is aba (written twice), even though aba itself starts and ends with a. In abcab, no shorter block works, so the answer is 5.

Example 1

Input

4
abcabcabc
abab
abaab
zzzzzz

Output

3
2
5
1

Example 2

Input

4
a
aabaab
abaaba
abcab

Output

1
3
3
5

Hints · reveal one at a time if you're stuck

Hint 1

If s is a block repeated k times, what does s look like when you shift it by the block length? Which shifts of s can match s itself?

Hint 2

The shortest such shift can be found in linear time with a prefix-function or Z-function computed on s, since the longest proper border of s gives the shortest shift. A shift only helps if it divides the length of s, so check that before answering.

report an issue Prefer your own editor? sooks prep 127 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.