#include <bits/stdc++.h>
using namespace std;
const int MX = 1e4 + 10;
int SR[14][MX], LCP[MX];
char text[MX];
struct myTuple{ int fir, sec, idx; };
myTuple SA[MX];
bool cmp(myTuple a, myTuple b){
return (a.fir == b.fir) ? a.sec < b.sec : a.fir < b.fir;
}
void build_SA(){
memset(SR, 0, sizeof(SR));
int N = strlen(text);
for(int i = 0; i<N; i++) SR[0][i] = text[i] - 'a';
for(int cnt = 1, stp = 1; cnt < N; cnt <<= 1, stp++){
for(int i = 0; i < N; i++){
SA[i].fir = SR[stp - 1][i];
SA[i].sec = (i + cnt) < N ? SR[stp - 1][i + cnt] : -1;
SA[i].idx = i;
}
sort(SA, SA + N, cmp);
SR[stp][SA[0].idx] = 0;
for(int i = 1, cr = 0; i < N; i++){
if(SA[i - 1].fir != SA[i].fir || SA[i - 1].sec != SA[i].sec) cr++;
SR[stp][SA[i].idx] = cr;
}
}
}
void build_LCP(){
memset(LCP, 0, sizeof(LCP));
int N = strlen(text), mx = 0;
for(int i = 1; i<N; i++){
int i1 = SA[i - 1].idx;
int i2 = SA[i].idx;
for(int j = 13; j >= 0; j--){
if(SR[j][i1] == SR[j][i2] && SR[j][i2]){
LCP[i] += (1 << j);
i1 += (1 << j);
i2 += (1 << j);
}
}
mx = max(mx, LCP[i]);
}
printf("%d\n", mx);
}
int main() {
int t;
scanf("%d", &t);
for(int i = 1; i <= t; i++){
memset(text, 0, sizeof(text));
scanf("%s", text);
int N = strlen(text);
text[N] = '$';
build_SA();
build_LCP();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTVggPSAxZTQgKyAxMDsKaW50IFNSWzE0XVtNWF0sIExDUFtNWF07CmNoYXIgdGV4dFtNWF07CgpzdHJ1Y3QgbXlUdXBsZXsgaW50IGZpciwgc2VjLCBpZHg7IH07Cm15VHVwbGUgU0FbTVhdOwoKYm9vbCBjbXAobXlUdXBsZSBh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