Under construction

Still being built. Things may change

Square Substring

Time limit0.5sMemory limit1024 MB

Summary
Given a string S, find the longest substring of S whose first half equals its second half, and print it.
Level

Medium5 of 10

Topics
Dynamic programming, String
Solved
No attempts yet

Problem

A string TT of length 2n2n is called a square string if its prefix of length nn and its suffix of length nn are equal. The empty string is a square string.

Given a string SS, print the longest string that is both a substring of SS and a square string. A substring of SS is a string obtained by removing some characters from SS without changing the order of the remaining characters.

Input

The first line contains the number of test cases TT.

Each of the next TT lines contains a string SS. SS consists only of lowercase English letters.

The length of each string is between 1 and 3000, inclusive. The total length of all given strings is at most 3000.

Output

For each test case, print the answer as follows.

If the answer is the empty string, print Case #T: 0 on one line.

If the answer is not the empty string, print Case #T: L on the first line, and print the answer on the next line.

Here TT is the test case number (starting from 1), and LL is the length of the answer.

Examples1

  1. Example 1

    Input
    5
    abba
    abbab
    abac
    abcd
    bbabab
    
    Expected output
    Case #1: 2
    aa
    Case #2: 4
    abab
    Case #3: 2
    aa
    Case #4: 0
    Case #5: 4
    bbbb