This page is still under construction.

Parts of this page are still being built. What you see may change.

Paper Folding

Time limit1sMemory limit128 MB

Summary
Repeatedly fold the left part of a binary strip over the right where symbols match and find the shortest reachable length.
Level

Medium6 of 10

Topics
Dynamic programming, String, Recursion
Solved
No attempts yet

Problem

Hektor is very bored during class, so he invented a game to keep himself busy. He cut out a strip of paper and wrote a string of 0s and 1s on it (for example 10000101011). Now he folds the strip between two adjacent symbols so that the folded part lines up with the part it is folded onto. The rule is that the symbols in the overlapping region must be equal. Hektor always folds the left side over to the right: the segment to the left of the crease is flipped and laid on top of the segment to its right.

For example, folding 10000101011 between the third and fourth symbols yields 00101011, and folding it between the second-to-last and last symbols yields 1010100001. After a fold, the length of the strip becomes the length of the longer of the two segments.

Hektor wants to fold the strip (possibly many times) so that it ends up as short as possible. For instance, 10011001 can be reduced to 01 (length 22): first fold between the fourth and fifth symbols to get 1001, then fold between the second and third symbols.

Determine the shortest length the strip can reach through folding.

Input

The first line contains an integer tt, the number of test cases (1≤t≤201 \le t \le 20). Then tt test cases follow.

Each test case consists of a single line containing the description of Hektor's strip: a string of 0s and 1s with no separators. The string has length between 11 and 100100 inclusive.

Output

For each test case, print on its own line a single integer: the length of the shortest strip obtainable by folding (possibly multiple times).

Examples3

  1. Example 1

    Input
    3
    11111111111
    10011001
    101
    
    Expected output
    1
    2
    3
    
  2. Example 2

    Input
    6
    0
    1
    01
    10
    00
    11
    
    Expected output
    1
    1
    2
    2
    1
    1
    
  3. Example 3

    Input
    4
    0101010101
    1010
    010
    0110
    
    Expected output
    10
    4
    3
    2