#include<stdio.h>
#include<string.h>
const int maxn = 1e6 + 5;
int nt[maxn];
char s[maxn];
// ekmp
void getNext(char* s, int* next) {
int i = 0, j, po, len = strlen(s);
next[0] = len;
while (s[i] == s[i+1] && i + 1 < len) i++;
next[1] = i;
po = 1;
for (int i = 2; i < len; i++) {
if (next[i-po] + i < next[po] + po) next[i] = next[i-po];
else {
j = next[po] + po - i;
if (j < 0) j = 0;
while (i + j < len && s[j] == s[j+i]) j++;
next[i] = j;
po = i;
}
}
}
int main() {
int T;
scanf("%d", &T);
long long sum; // 用int会wa
while (T--) {
scanf("%s", &s);
getNext(s, nt);
sum = 0;
int len = strlen(s);
for (int i = 1; i < len; i++) {
sum += nt[i];
if (nt[i] + i != len)
sum++;
}
printf("%lld\n", sum);
}
return 0;
}