#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
char a[N];
vector<int> v;
inline ll square(ll x) { return x * x; }
int main()
{
    int T;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++)
    {
        v.clear();
        scanf("%s", a);
        int n = strlen(a);
        int curLen = 1;
        for (int i = 1; i < n; i++)
        {
            if (a[i - 1] == a[i])
                curLen++;
            else
                v.push_back(curLen), curLen = 1;
        }
        v.push_back(curLen);
        /*for (int i : v)
            cout << i << " ";
        cout << endl;*/
        ll ans_base = accumulate(v.begin(), v.end(), 0ll, [](ll pr, ll x) {
            return pr + x * x;
        });
        ll ans = ans_base;
        if (v[0] == 1)
            ans = max(ans, ans_base - 1 - square(v[1]) + square(1 + v[1]));
        if (v[v.size() - 1] == 1)
            ans = max(ans, ans_base - 1 - square(v[v.size() - 2]) + square(1 + v[v.size() - 2]));
        for (int i = 1; i < v.size() - 1; i++)
            if (v[i] == 1)
                ans = max(ans, ans_base - 1 - square(v[i - 1]) - square(v[i + 1]) + square(v[i - 1] + 1 + v[i + 1]));
        for (int i = 1; i < v.size(); i++)
            ans = max(ans, ans_base - square(v[i - 1]) - square(v[i]) + square(v[i - 1] + 1) + square(v[i] - 1));
        for (int i = 1; i < v.size(); i++)
            ans = max(ans, ans_base - square(v[i - 1]) - square(v[i]) + square(v[i - 1] - 1) + square(v[i] + 1));
        printf("Case #%d: %lld\n", t, ans);
    }
    return 0;
}