#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
char str[N];
int nxt[N], T;
int main()
{
    nxt[0] = -1;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%s", str);
        int len = strlen(str);
        for (int i = 0, j = -1; i < len;)
            if (~j && str[j] != str[i])
                j = nxt[j];
            else
                nxt[++i] = ++j;
        int ans = len - nxt[len];
        printf("%d\n", (len % ans == 0 && ans != len) ? 0 : ans - len % ans);
    }
    return 0;
}