#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const int MAX_N = 105;
int T, N;
char S[MAX_N];
int Z[MAX_N][MAX_N], f[MAX_N][MAX_N];
long long dp0[MAX_N], dp[MAX_N][MAX_N][MAX_N];
inline void imin(long long &x, long long y) {
x = min(x, y);
x = min(x, INF);
}
int solve() {
for(int i=0; i<=N; i++) for(int j=0; j<=N; j++) for(int k=0; k<=N; k++)
dp[i][j][k] = INF;
for(int i=0; i<=N; i++)
dp0[i] = INF;
dp0[0] = 0;
for(int i=1; i<=N; i++) {
int x, y;
// paste some substring S[j,i]
for(int j=1; j<=i; j++) {
x = f[j][i], y = x + i-j;
imin(dp[i][x][y], dp[j-1][x][y] + 1);
imin(dp0[i], dp[i][x][y]);
}
// type char S[i]
imin(dp0[i], dp0[i-1] + 1);
for(int j=1; j<i; j++) for(int k=1; k<=j; k++)
imin(dp[i][k][j], dp[i-1][k][j] + 1);
// update
for(int j=1; j<=i; j++) for(int k=1; k<=j; k++)
imin(dp[i][k][j], dp0[i] + 1);
}
}
// codeforces.com/blog/entry/3107
void setZ() {
for(int st=1; st<=N; st++) {
int L = st, R = st;
Z[st][st] = N - st + 1;
for(int i=st+1; i<=N; i++) {
if(i>R) {
L = R = i;
while(R<=N && S[st+R-L]==S[R]) R++;
Z[st][i] = R - L; R--;
}
else {
int k = st + i - L;
if(Z[st][k]<R-i+1) Z[st][i] = Z[st][k];
else {
L = i;
while(R<=N && S[st+R-L]==S[R]) R++;
Z[st][i] = R - L; R--;
}
}
}
}
}
void setf() {
for(int i=1; i<=N; i++) for(int j=1; j<=i; j++) {
for(int st=j; st>=1; st--)
if(Z[st][j] >= i-j+1)
f[j][i] = st;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> T;
for(int t=1; t<=T; t++) {
cin >> S+1;
N = strlen(S+1);
setZ();
setf();
solve();
cout << "Case #" << t << ": " <<dp0[N] << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysr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TE7IGs8PWo7IGsrKykKICAgICAgICAgICAgaW1pbihkcFtpXVtrXVtqXSwgZHBbaS0xXVtrXVtqXSArIDEpOwogICAgICAgIAogICAgICAgIC8vIHVwZGF0ZQogICAgICAgIGZvcihpbnQgaj0xOyBqPD1pOyBqKyspIGZvcihpbnQgaz0xOyBrPD1qOyBrKyspCiAgICAgICAgICAgIGltaW4oZHBbaV1ba11bal0sIGRwMFtpXSArIDEpOwogICAgICAgIAogICAgfQp9CgovLyBjb2RlZm9yY2VzLmNvbS9ibG9nL2VudHJ5LzMxMDcKdm9pZCBzZXRaKCkgewogICAgZm9yKGludCBzdD0xOyBzdDw9Tjsgc3QrKykgewogICAgICAgIGludCBMID0gc3QsIFIgPSBzdDsKICAgICAgICBaW3N0XVtzdF0gPSBOIC0gc3QgKyAxOwogICAgICAgIGZvcihpbnQgaT1zdCsxOyBpPD1OOyBpKyspIHsKICAgICAgICAgICAgaWYoaT5SKSB7CiAgICAgICAgICAgICAgICBMID0gUiA9IGk7CiAgICAgICAgICAgICAgICB3aGlsZShSPD1OICYmIFNbc3QrUi1MXT09U1tSXSkgUisrOwogICAgICAgICAgICAgICAgWltzdF1baV0gPSBSIC0gTDsgUi0tOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgaW50IGsgPSBzdCArIGkgLSBMOwogICAgICAgICAgICAgICAgaWYoWltzdF1ba108Ui1pKzEpIFpbc3RdW2ldID0gWltzdF1ba107CiAgICAgICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBMID0gaTsKICAgICAgICAgICAgICAgICAgICB3aGlsZShSPD1OICYmIFNbc3QrUi1MXT09U1tSXSkgUisrOwogICAgICAgICAgICAgICAgICAgIFpbc3RdW2ldID0gUiAtIEw7IFItLTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKdm9pZCBzZXRmKCkgewogICAgZm9yKGludCBpPTE7IGk8PU47IGkrKykgZm9yKGludCBqPTE7IGo8PWk7IGorKykgewogICAgICAgIGZvcihpbnQgc3Q9ajsgc3Q+PTE7IHN0LS0pCiAgICAgICAgICAgIGlmKFpbc3RdW2pdID49IGktaisxKQogICAgICAgICAgICAgICAgZltqXVtpXSA9IHN0OwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKE5VTEwpOwogICAgCiAgICBjaW4gPj4gVDsKICAgIGZvcihpbnQgdD0xOyB0PD1UOyB0KyspIHsKICAgICAgICBjaW4gPj4gUysxOwogICAgICAgIE4gPSBzdHJsZW4oUysxKTsKICAgICAgICAKICAgICAgICBzZXRaKCk7CiAgICAgICAgc2V0ZigpOwogICAgICAgIHNvbHZlKCk7CiAgICAgICAgCiAgICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCB0IDw8ICI6ICIgPDxkcDBbTl0gPDwgIlxuIjsKICAgIH0KICAgIHJldHVybiAwOwp9