We are given the problem description: There are T test cases. For each test case, we are given a string A. There are two strings: first string is A, second string is the reverse of A. We need to find the longest substring which is the prefix of both strings. That is, we have two strings: S = A, and T = reverse(A). We need the longest string X such that X is a substring (contiguous) of S? Wait careful: "find the longest substring which is the prefix of both strings". The phrasing: "he gave him a two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of both strings." The wording ambiguous: It might be: given two strings (first string and its reverse), find the longest substring that is a prefix of both strings. But "substring" and "prefix" both applied to the two strings? Typically, a "prefix" is a substring that starts at the beginning. So we are asked: find the longest string that is a prefix of the first string and also a prefix of the second string. That's just the longest common prefix (LCP) between the first string and its reverse. But that would be trivial: we could just compute the common prefix length between A and reverse(A). However, note that the sample input and output:
Input:
3
abababa
tipra
manish
Output:
Case 1: 5
Case 2: -1
Case 3: -1
Let's test: A = "abababa". Reverse = "abababa". Actually reverse is also "abababa" because it's palindrome? "abababa" is palindrome? Let's check: original: a b a b a b a, reverse: a b a b a b a, indeed palindrome. So the longest common prefix between A and reverse(A) would be the whole string length = 7. But output says 5. So it's not that.
Maybe it is: "longest substring which is the prefix of both strings" meaning: we need to find a string X that is a substring of A? Or "substring" refers to something else? Let's parse again: "he gave him a two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of both strings." Possibly means: Given the two strings (first and its reverse), find the longest string that is simultaneously a substring of the first string and a prefix of the second string? Or simultaneously a substring of both? The phrase "substring which is the prefix of both strings" could be interpreted as: we consider substrings of something? Actually it says "longest substring which is the prefix of both strings". That suggests we look for a string that is a substring (maybe of something else?) and that substring is also a prefix of both the given strings. That doesn't quite fit.
Maybe there is miswording: Perhaps it means: given two strings (the original and its reverse), we need to find the longest substring that appears as a prefix in both strings. i.e., find the longest common prefix that is also a substring of the original? No.
Alternate possibility: Actually reading the story: "He asked him to find the longest substring which is the prefix of both strings." The two strings are the original and its reverse. So we need to find a string X such that X is a substring of ... wait, "substring which is the prefix of both strings" means X is a substring of something? The phrase "substring" is likely a misnomer; maybe they meant "string" that is a prefix of both. But that would be LCP. But sample contradicts.
Check sample: abababa -> answer 5. So what's length 5? Possibly the longest palindromic prefix? That is "aba"? length 3? Not 5. Maybe longest prefix that is also a suffix? That is "aba"? KMP border length? For "abababa", the longest border is "ababa"? Actually "ababa" is a prefix and suffix? Prefix: "ababa", suffix of length 5: "bababa"? suffix of length 5 would be last 5 chars: "bababa"? Wait string length 7: indices 0-6: a b a b a b a. Suffix of length 5: positions 2-6: a b a b a? Actually careful: indices: 0:a,1:b,2:a,3:b,4:a,5:b,6:a. Suffix of length 5: indices 2..6: a,b,a,b,a? That's "ababa"? That matches prefix "ababa" (indices 0-4: a,b,a,b,a). Yes, prefix "ababa" is a prefix of length 5 and suffix of length 5 is also "ababa". So longest border length is 5. So maybe the problem is to find the longest border (i.e., longest proper prefix which is also a suffix) of the original string? But the problem statement mentions two strings: original and its reverse. Why bring reverse? Perhaps they want the longest common substring between the original and its reverse that is also a prefix of both? Hmm.
Maybe they want: Given string A and its reverse rev(A), consider all substrings that are common to both A and rev(A). Among those, find the longest substring that is also a prefix of A (or prefix of both? maybe of either). But the phrase "prefix of both strings" suggests that the substring must be a prefix of A and a prefix of rev(A). That would be LCP as earlier.
But sample output 5 for "abababa". If we compute LCP of "abababa" and its reverse (which is same), length=7. So not 5. If we compute longest common substring between A and rev(A) that is also a prefix of A, what would that be? For palindrome strings like this, the longest common substring that is a prefix of A? Since A and rev(A) are identical, any substring of A appears in rev(A). The longest substring that is also a prefix of A is simply A itself, length 7. So still not 5.
Maybe they want the longest substring that is simultaneously a prefix of the first string and a suffix of the first string? But that's border. And the border length for "abababa" is 5 (since prefix of length 5 equals suffix of length 5). And note: The reverse string is involved? Could be a trick: The second string is the reverse of the first. If we want a string that is a prefix of both, that is equivalent to a prefix of the original that is also a prefix of the reverse. But a prefix of the reverse is equivalent to a suffix of the original reversed? Actually, prefix of reverse(A) corresponds to suffix of A reversed. So condition: X is prefix of A and prefix of reverse(A) means X = some string such that X = A[0..k-1] and X = reverse(A)[0..k-1] = reverse( A[-k..] )? Actually reverse(A)[0..k-1] = (A[n-1], A[n-2], ..., A[n-k]) i.e., the reverse of the suffix of length k. So X equals the reverse of the suffix of length k. Therefore, X being prefix of A and of reverse(A) is equivalent to: A[0..k-1] = reverse( A[n-k..n-1] ). Or equivalently, A[0..k-1] is equal to the reverse of the suffix of length k. That's not the same as border. For a palindrome, the condition would be A[0..k-1] = reverse( A[n-k..n-1] ) which means A[0..k-1] is a palindrome? Actually if A is palindrome, then suffix is reverse of prefix, so A[0..k-1] = reverse( A[n-k..n-1] ) holds for any k? Let's check: For palindrome, A = reverse(A). Then reverse(A)[0..k-1] = A[0..k-1]? Actually reverse(A) is same as A, so prefix of reverse(A) is same as prefix of A. So condition becomes A[0..k-1] = A[0..k-1] automatically true. So LCP length would be full length. So sample still not matching.
Wait sample says for "abababa" answer 5. That suggests maybe they consider "substring" as a proper substring not necessarily starting at beginning. Perhaps they want the longest substring (contiguous) that appears as a prefix in both strings? That means we need to find a string X that is a substring of A? Actually phrase: "longest substring which is the prefix of both strings". Could be interpreted as: among all substrings of A (or maybe of any string), we want the one that is simultaneously a prefix of the first string and a prefix of the second string. But "substring" doesn't have an obvious reference string. Maybe they meant "string" (just a string) that is a prefix of both strings. But that is LCP.
Maybe there is missing context: The problem might be from some known programming problem. Possibly it's about the longest common substring between the original and its reverse that is also a prefix of the original? I recall a problem: "Given a string, find the longest substring which is a prefix of the string and a suffix of the string" (border). But here they bring reverse. Or maybe it's: "Given two strings, one is reverse of the other, find the longest substring which is a prefix of the first and a suffix of the second" which is essentially border if you think about it? Because prefix of first and suffix of second? But second is reverse of first, so suffix of reverse is prefix of original? Let's derive: Let S = A, T = reverse(A). Want longest X such that X is a prefix of S and a suffix of T? Or something like that.
The sample: A="abababa". Compute longest X that is prefix of S and suffix of T? Suffix of T is like last part of reverse(A). Reverse(A) last characters correspond to first characters of A. Actually suffix of reverse(A) of length k = reverse(A)[n-k..n-1] = reverse of prefix of A of length k. So condition X = prefix of A, and X = suffix of reverse(A) => X = reverse of prefix of A? So X must equal both prefix of A and reverse(prefix of A). So that would mean prefix of A is palindrome. So we need longest prefix of A that is palindrome. For A="abababa", the prefixes: "a" (palindrome), "ab" not, "aba" yes (3), "abab" no, "ababa" yes (5), "ababab" no, "abababa" yes (7). Longest palindrome prefix is 7. But answer is 5. So not that.
What about X being prefix of both? Already considered, gives 7.
What about X being substring of A that is a prefix of reverse(A)? That seems weird.
Maybe the problem actually is: Given string A, and its reverse B, find the longest common substring between A and B. But then sample: For "abababa", A and B are same, longest common substring is entire string length 7, not 5. So no.
Maybe there's constraint that the substring must be proper? But no -1 for others.
Check second sample: "tipra". A = "tipra". Reverse = "arpit". Longest common prefix of A and reverse = compare "t" vs "a": no match, so length 0. But output says -1. So if no such substring, output -1. So length 0 yields -1. That's plausible if they consider length 0 not valid.
But for "manish": A="manish", reverse="hsinam". LCP length 0? Actually first char m vs h: no match, so 0. Output -1.
So for those two, answer -1 matches LCP of 0. For "abababa", LCP length 7, but output 5. So there's a discrepancy.
Perhaps they want the longest proper prefix that is also a suffix? i.e., border. For "abababa", border lengths: LPS array from KMP: [0,0,1,2,3,4,5] maybe? Actually compute border lengths: For "abababa", the longest proper prefix that is also suffix: length 5 because "ababa" matches? But check: prefix length 5 = "ababa", suffix length 5 = last 5 chars: indices 2-6: "ababa"? Actually confirm: suffix of length 5 of "abababa" is from index 2 to 6: characters: index2=a,3=b,4=a,5=b,6=a => "ababa". Yes matches. Also length 3: "aba", length 1: "a". So longest border is 5 (since full length 7 is not proper). If they require proper (i.e., not the whole string), then answer 5. For "tipra": borders? Check: "tipra" any prefix equals suffix? prefix "t" vs suffix "a": no; "ti" vs "ra": no; "tip" vs "pra": no; "tipr" vs "ipra"? no. So border length 0 -> -1. For "manish": similar border length 0 -> -1. So matches sample! Indeed sample matches border.
But why mention reverse? Possibly because the second string is the reverse of the first, and they want longest substring that is a prefix of both strings. But for a border? Let's examine: For a string S, its reverse is R. A string X that is a prefix of S and also a prefix of R. That means X = S[0..k-1] and X = R[0..k-1]. But R[0..k-1] = reverse of S suffix of length k? Actually R[0..k-1] = S[n-1]...S[n-k]. So condition is S[0..k-1] = reverse( S[n-k..n-1] ). That is not border. However, if S is palindrome? Not general.
But maybe the problem originally meant: given two strings, the second is the reverse of the first, find the longest common substring that is a prefix of the first and a suffix of the second? That becomes border? Let's test: prefix of first (S) and suffix of second (R). Suffix of R of length k = R[n-k..n-1] = reverse of S[0..k-1]? Actually R[n-k..n-1] = reverse of S[0..k-1]? Let's compute: R is reverse of S. R[i] = S[n-1-i]. So suffix of R from n-k to n-1 corresponds to indices i from n-k to n-1 in R. Those correspond to S indices: for each i in that range, S index = n-1-i, which will range from n-1-(n-1)=0 to n-1-(n-k)=k-1. So indeed suffix of R of length k is reverse of prefix of S of length k. So condition: X is prefix of S and suffix of R => X = prefix_k(S) and X = suffix_k(R) = reverse(prefix_k(S)). So X must equal its reverse => X palindrome. So it would be longest palindrome prefix of S. For "abababa", that's 7. Not 5.
What about X being substring (of S maybe) that is prefix of both S and R? That's LCP we considered.
Maybe the intended problem: "Given a string, find the length of the longest proper prefix which is also a suffix." That's classic KMP failure function. The sample matches. But why bring reverse? Could be a red herring or mis-statement. Let's read problem statement again as provided: "Arpit thought AB De-villiers could do anything no matter what it is, however his brother didn’t believe him . To make his brother believe him he contacted AB and gave him a problem in which he gave him a two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of both strings . Unfortunately the call ended but to prove AB himself being master in everything he calls him back and tells him the answer . What is the answer ?"
Maybe it's a twist: The "longest substring which is the prefix of both strings" might be interpreted differently: There are two strings: original and its reverse. We are to find the longest substring that is a prefix of both strings. But "substring" could refer to the original string? That is, find a substring of the original string (i.e., a contiguous part of it) that is also a prefix of both strings. That would be: We need a substring X of A such that X is a prefix of A and also a prefix of reverse(A). But X being a substring of A and also a prefix of A means that X must start at index 0? Actually a substring that is also a prefix must be a prefix itself (starting at index 0). Because any prefix is a substring starting at index 0. So it's equivalent to a prefix. So again LCP.
Alternatively, maybe they mean: find the longest string that is both a substring of the first string and a prefix of the second string, and also a prefix of the first string? No.
Given that sample matches border, perhaps the problem actually asks for the longest border: longest proper prefix which is also a suffix. But the mention of reverse might be because if you reverse the string, a prefix of original becomes suffix of reverse, so the longest common substring that is a prefix of original and a prefix of reverse maybe equivalent to something else? Let's try to link border to reverse: Consider original S and reverse R. What is the longest string X such that X is a prefix of S and also a suffix of S? That's border. But how does R come in? If X is a suffix of S, then reverse(X) is a prefix of R. So border condition: X is prefix of S and suffix of S => X is prefix of S and reverse(X) is prefix of R. Not exactly.
What if we consider X being a prefix of both S and R? That gave LCP.
What if we consider X being a substring of S that appears as a prefix in both S and R? That again requires X starting at 0.
Thus border seems unrelated to reverse.
Maybe the problem originally: "He gave him two strings where the second string was the reverse of the first. He asked him to find the longest substring which is a prefix of the first and a suffix of the second." But then we derived palindrome prefix, which would be length 7 for "abababa", not 5.
Unless they considered proper (not full) as well. If they considered "substring" must be proper (not equal to whole string), then for "abababa" the longest proper palindrome prefix would be "ababa" length 5 (since full length is palindrome but maybe they exclude whole string). But then why would they exclude full string? The problem says "longest substring" without explicit "proper", but maybe by substring they mean proper substring? Usually a substring can be the whole string. But they say "prefix of both strings", prefix can be the whole string as well. So no.
Let's test "abababa" for palindrome prefix proper: lengths 5,3,1. Longest is 5. So matches sample. For "tipra", palindrome prefixes? "t" is palindrome (single character always palindrome). But output is -1. So if they considered palindrome prefixes, "t" length 1 should be valid, not -1. Unless they require length>1? But still -1 would not be output. So it's not palindrome prefix.
Thus border seems plausible because border length must be strictly less than n, and for "tipra", border length is 0 -> -1, for "manish" border 0 -> -1. For "abababa", border length 5 -> output 5. So matches.
But what about "a"? If test case "a", border length 0 -> -1. Palindrome prefix length 1 would be output 1? Not sure.
Maybe there is an alternative: The problem might be to find the longest common substring between the string and its reverse that is also a prefix of the original string. For "abababa", the original and reverse are same, so any substring qualifies. The longest such that is also a prefix is the whole string, length 7. Not 5.
Unless they consider proper substring (not whole). Then longest proper substring that is a prefix is prefix of length n-1 =6? But 6 not border. Could be anything? Actually prefix of length 6 is "ababab". That is a substring obviously. It is also prefix of original. So answer would be 6. Not 5.
Thus not that.
Maybe it's the longest proper prefix that is also a suffix of the reverse? Let's test: For S, R = reverse(S). Condition: X prefix of S and suffix of R? We already did, yields palindrome prefix. For "abababa", full length is palindrome, but maybe proper: length 5 palindrome prefix works. But for "tipra", prefix "t" is palindrome, so length 1 would be valid, so not -1. So no.
Thus border remains the only match.
Let's double-check border for "tipra": Compute using KMP prefix function. "tipra": prefix function array: at each position i (0-indexed), length of longest proper prefix of substring ending at i that is also suffix.
- i=0: "t": 0
- i=1: "ti": no proper border: 0
- i=2: "tip": 0
- i=3: "tipr": 0
- i=4: "tipra": 0
So border length = prefix function at last index = 0. So no border. So -1.
For "manish": similarly 0.
For "abababa": compute prefix function:
i=0:0
i=1: "ab":0
i=2: "aba": border "a" length1 ->1
i=3: "abab": border "ab" length2 ->2
i=4: "ababa": border "aba" length3? Actually "ababa" has prefix "aba" and suffix "aba"? Check: prefix of length3="aba", suffix of length3="aba" (positions 2-4?), careful: substring "ababa" indices 0-4: a b a b a; suffix of length3 is indices 2-4: a b a => "aba". So border length 3. Also maybe "a" length1, but longest is 3.
i=5: "ababab": border? prefix length? Possibly "abab"? Check prefix of length4="abab", suffix length4 = "baba"? Not equal. "aba"? suffix length3="bab"? no. "ab"? suffix length2="ab"? Actually suffix length2 = last 2 chars "ab"? yes "ab". So border length 2? Actually "ab" length2 matches. Also maybe "abab"? no. So border length 2? Wait check more carefully: "ababab": characters: a b a b a b. Suffix length 4: "b a b ?" Actually last 4: positions 2-5: a b a b? Wait indices: 0:a,1:b,2:a,3:b,4:a,5:b. Suffix length4: indices 2-5: a,b,a,b => "abab". That's equal to prefix of length4 "abab". So border length 4! Indeed prefix "abab" vs suffix "abab"? suffix "abab" is a b a b? Yes that's "abab". So border length 4. So prefix function for i=5 would be 4.
i=6: "abababa": prefix length5="ababa" and suffix length5="ababa"? last 5: indices 2-6: a b a b a => "ababa". So border length 5. So indeed border length 5. So matches sample.
Thus sample output consistent with border length.
Now, is there any known problem linking border and reverse? Perhaps the problem is to find the longest common substring between the string and its reverse that is also a prefix of the original. But that's border? Let's see: Consider a border X of S: X is a prefix and also a suffix. Reverse of suffix? Hmm.
If X is a border, then X is a suffix of S. Reverse(X) is a prefix of reverse(S). So X and reverse(X) are prefixes of reverse? Actually, X being suffix of S means reverse(X) is prefix of reverse(S). So if we find X such that X = reverse(X) (i.e., X palindrome), then X is also a prefix of reverse(S). But border X is not necessarily palindrome. So not that.
However, there is known trick: The longest prefix of S that appears as a substring in reverse(S) is maybe related to border? Not exactly.
Maybe the original problem statement is actually: "Given a string, find the longest substring which is a prefix of the string and a suffix of the string." That's exactly the border. But why mention reverse? Could be a mistake in the statement provided here. Alternatively, maybe the problem originally was: "He gave him two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of first and suffix of second." That would be border? Let's test: prefix of first S and suffix of second R. As we derived, that yields X = prefix of S and X = suffix of R. But suffix of R is reverse of prefix of S. So that forces X to be palindrome. That is not border.
But what if we consider substring (not necessarily prefix) that is common to both S and R and also a prefix of S? Not border.
Thus border is simpler and matches sample.
Given the constraints: t<10, length up to 100000. For each test case, we need to compute something efficiently. For border, we can compute using KMP prefix function in O(n). For LCP, O(n) as well. But border also O(n).
Sample output format: "Case 1: 5". So we need to output in format "Case i: answer" where i starts from 1.
Thus I'm leaning that the intended problem is: Given a string, find the length of the longest proper prefix which is also a suffix. But they inserted a story about reverse to confuse.
But wait, the problem statement: "he gave him a two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of both strings." Possibly they mean: There are two strings (original and its reverse). Among all substrings that are common to both strings, find the one that is a prefix of the original string. But "prefix of both strings" means it's a prefix of original and also a prefix of reverse? That is LCP as we said. Not border.
Alternatively, maybe they mis-worded: "longest substring which is the prefix of the first and suffix of the second" because that involves reverse in a natural way: prefix of first and suffix of second (which is reverse) is like checking palindrome prefix? Not border.
But sample says border. So either the sample is from a different problem, or I need to reinterpret.
Let's test if any other interpretation yields 5 for "abababa" and -1 for others.
Interpretation 1: Longest palindromic substring that is also a prefix of the string? For "abababa", the longest palindromic prefix is whole string (7). Not 5.
Interpretation 2: Longest common substring between string and its reverse that is also a prefix of the string, but perhaps they require that substring not equal to the whole string? Then for "abababa", the longest such substring that is a prefix but not whole string could be any prefix that appears somewhere in the reverse as a substring? Since reverse is same, any prefix appears as substring (the prefix itself). So longest prefix less than n would be length 6. Not 5.
Interpretation 3: Longest substring that is a prefix of the string and also a prefix of the reversed string. That's LCP. For "abababa", LCP length 7, but if they require proper (i.e., not full length), then 6? But 6's prefix "ababab" compared to reverse's prefix length 6: reverse is same, so they match. So 6 would be possible if full not allowed. But they output 5, so not that.
Interpretation 4: Longest border (prefix that is also suffix). That's 5. For "abababa", border length 5. For "tipra", border 0 => -1. For "manish", border 0 => -1. So matches.
But is there any connection to reverse? Could be that they compute the border by using the original and its reverse? For example, you can compute the border by concatenating the string and its reverse and then something? Not exactly.
Maybe they intended: Given two strings S and rev(S), find the longest common substring that starts at the beginning of S and also at the beginning of rev(S). That is exactly LCP. But that doesn't match.
Let's check sample more carefully: Input:
3
abababa
tipra
manish
Maybe they expect for "abababa": 5 because the longest common prefix of "abababa" and "abababa" (the reverse) is 7, but they output 5. So maybe they exclude cases where the entire string is palindrome? That seems odd.
Maybe they consider "substring" as contiguous within the string that is a prefix of both strings, but not necessarily starting at the beginning of the string? For example, in the string "abababa", consider substrings (contiguous sequences) of it. Among these substrings, find those that are also a prefix of both the original and its reverse. That means we need a string X that appears somewhere inside A (as a substring) such that X is a prefix of A and also a prefix of reverse(A). Since X must be prefix of A, X must be exactly a prefix of A. But it also must appear as a substring somewhere else? The phrase "substring which is the prefix of both strings" could be read as: we have two strings; we need to find a string that is a substring of both strings, and also that substring is a prefix of both strings. But "substring" might refer to being a substring of either? Actually typical phrasing: "longest common substring which is also a prefix of both strings" would mean among all common substrings, pick one that is a prefix of both strings. However, a common substring that is a prefix of both strings is simply the LCP (since any common substring that is a prefix of both must start at index 0 in both). So again LCP.
But maybe they consider "substring" of the original? They say "longest substring which is the prefix of both strings". The phrase could be parsed as: [the longest substring] which is the prefix of both strings. That is, we want a substring (of something unspecified) that serves as a prefix for both strings. But which substring? Possibly they mean a substring of the first string that is also a prefix of the second string? The wording is ambiguous.
If we interpret: We have two strings A and B (B = reverse(A)). Find the longest substring of A that is also a prefix of B. That is a known problem: find the longest prefix of B that appears as a substring in A. Since B = reverse(A), we are looking for the longest prefix of reverse(A) that appears as a substring in A. For "abababa", reverse(A) = "abababa". Its prefixes are: "a", "ab", "aba", "abab", "ababa", "ababab", "abababa". Which of these appear as substrings in A? All do because A = reverse(A). So the longest is whole string length 7. But output 5.
What if we consider longest substring of A that is also a prefix of A and a prefix of B? That again reduces to prefix.
Maybe we need to consider the intersection of prefixes? Not.
Given the sample's perfect match with border, I think it's safe to conclude that the problem is to compute the length of the longest proper prefix of the string that is also a suffix. However, the mention of reverse may be a distraction. But the problem statement explicitly says "two strings where the second string was the reverse of the first". Could it be that the border condition can be rephrased using reverse? Let's see: Suppose we have string S and its reverse R. The condition "X is a prefix of S and also a suffix of S" can be translated to "X is a prefix of S and reverse(X) is a prefix of R". Not symmetric.
Maybe the problem originally asked: "Given two strings, the second being the reverse of the first, find the longest common substring between them that is also a prefix of the first string." For S=abababa, R=abababa, common substrings: any. The longest common substring that is a prefix of S is S itself. So length 7. Not 5.
Unless they exclude overlapping? No.
I suspect that the problem statement in the prompt may be incorrectly transcribed from some source. The sample corresponds exactly to the "Prefix Function" or "KMP" problem of longest border. Many online judges have problems like "Longest Prefix Suffix" or "Find the length of the longest proper prefix which is also a suffix". Example: SPOJ problem LPS or similar. But the story with reverse is unusual.
Let's search memory: I recall a problem called "The Longest Prefix Which Is Also Suffix" sometimes appears as "Border". But there's also a problem: "Given a string, find the longest substring which is a prefix, a suffix and also appears in the middle." That's something else.
Alternatively, maybe the problem is: "Given a string, find the longest substring which is a prefix of the string and also a prefix of its reverse." But we already saw that's LCP. And sample doesn't match.
Wait: Could it be that they consider the "substring" as something that appears in both strings, but they misinterpret "prefix of both strings" as "it is a prefix of the first string, and (as a substring) it appears somewhere in the second string"? No, that would be "longest prefix of the first string that appears as a substring in the second". That is a different problem. For "abababa", the longest prefix of S that appears as substring in R (which is same) is whole S, length 7. Not 5. For "tipra", longest prefix of "tipra" that appears as substring in "arpit": Check prefixes: "t" appears in "arpit"? Yes? 't' appears at the end of "arpit"? "arpit" has 't' as last character, so yes. So length 1. "ti"? "ti" does not appear as substring in "arpit"? Let's see: "arpit" substrings: "ar","rp","pi","it". "ti" not present. So longest prefix appearing as substring is "t" length 1. Output would be 1, not -1. So not that.
What about "longest substring that appears as a prefix in both strings"? That's LCP.
Thus border is the only plausible.
Given that the sample matches border and the constraints are up to 100,000, it's a standard KMP prefix function O(n) per test.
Thus I'll implement solution that computes the longest proper prefix which is also a suffix. That is, for each string, compute the length of the longest border (also known as the value of the prefix function at the last index). If that length is 0, output -1; else output that length.
But wait: In KMP, the prefix function π[i] gives length of longest proper prefix of substring s[0..i] which is also suffix (but not counting the whole substring). For whole string, π[n-1] gives length of longest border that is not the whole string. So that matches "longest proper prefix which is also a suffix". So if π[n-1] == 0, output -1; else output π[n-1].
But is it guaranteed that they want proper? Usually "prefix" can be the whole string, but "longest substring which is the prefix of both strings" might allow the whole string. But sample: for "abababa", whole string is a prefix of itself and its reverse (both same), so length 7. But they output 5, so they exclude the whole string. So proper.
Now, check if there is any connection with reverse: If we have S and reverse(S), maybe the longest common prefix of S and reverse(S) is something else. For "abababa", that's 7. So not.
Maybe the problem wants the longest common substring between S and reverse(S) that is also a prefix of S, but also proper? For "abababa", the longest common substring that is a prefix of S is S (7). So no.
Thus I'll go with border.
But there's still a nuance: The problem states "the longest substring which is the prefix of both strings". If we interpret "substring" as any substring of the original string (or maybe of any string) that is a prefix of both strings. For the case where S is palindrome, any prefix of S is also a prefix of reverse(S) (since they are equal), so the longest such is the whole string. But they output 5. So they must be excluding the whole string, perhaps because they consider "substring" as a contiguous part of the original that is not necessarily starting at index 0? But a prefix always starts at 0. The only way a prefix can be a substring without being the whole string is that the substring might not be at the beginning? Wait, a prefix is defined as starting at index 0. So if we require X to be a substring of S (i.e., appears somewhere in S) and also a prefix of both S and reverse(S), then X being a prefix of S forces X to start at 0. So it is exactly a prefix of S. So it's either the whole string or a shorter prefix. For "abababa", the prefixes are lengths 1..7. The longest one that is also a prefix of reverse(S) (which is same) is length 7. So they'd output 7. So that fails.
Thus they must be requiring X to be a substring that is a prefix of both, but also proper? Still whole string qualifies and is longest, so 7.
Thus no.
Thus I'm convinced the intended is border, and the mention of reverse is either a mistake or there is a different interpretation that yields border.
Let's attempt to derive border from the given wording: "two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of both strings." Perhaps they meant "suffix" instead of "substring"? If it were "longest suffix which is the prefix of both strings"? That doesn't make sense.
What if it's "longest substring which is the prefix of the first and suffix of the second"? As we saw, that yields palindrome prefix. For "abababa", longest palindrome prefix is 7. Not 5.
What if it's "longest substring which is a prefix of the first and a prefix of the second's reverse"? Not.
Maybe it's "longest substring which is a prefix of the first and a substring of the second"? Not.
Alternatively, maybe the problem originally gave two strings: original and its reverse. Then they asked to find the longest common substring between them. That is a classic problem: longest common substring between a string and its reverse gives the longest palindromic substring (actually, not exactly; longest common substring between S and reverse(S) can be longer than longest palindromic substring? Typically, the longest common substring between S and reverse(S) gives the longest palindromic substring? Actually, it's known that the longest palindromic substring can be found by computing longest common substring between S and reverse(S) with constraint that indices correspond appropriately. However, directly computing LCS substring gives a string that is common, which is palindrome? If we have S and rev(S), a common substring X appears in S at some position and in rev(S) at some position. That X will be a palindrome? Not necessarily; consider S="abac", rev="caba". Common substrings: "aba" is palindrome; "c" not; "a". So it often gives palindrome. But the longest common substring between S and rev(S) is often used to find longest palindromic substring. For "abababa", longest common substring is the whole string (7). So that would be 7. Not 5.
So not that.
Maybe they ask for the longest substring that is a prefix of the original and also a substring of the reverse. That's same as longest prefix that appears in reverse. For "abababa", that's 7. Not 5.
Could it be that they ask for the longest proper prefix that is also a suffix of the reverse? Let's define: X is a prefix of A. X is also a suffix of reverse(A). As we derived, that means X is palindrome. For "abababa", palindrome prefixes: lengths 1,3,5,7. Longest proper is 5. So that yields 5! And for "tipra", palindrome prefixes: "t" is palindrome. So length 1. But output -1. So why would they output -1 for tipra? Because maybe they require X to be also a substring? Or maybe they require X to be a proper prefix that is also a suffix of reverse(A) and also that X is a substring of something else? Let's examine: For "tipra", palindrome prefixes: "t" (length 1) is palindrome. So if they output -1, they must be excluding length 1? Possibly they consider length 1 not valid because it's trivial? But then why not output -1 for "manish"? "m" is palindrome, length 1. So they'd output 1? But they output -1. So not that.
Unless they require that X be of length > 1? Still, for "tipra", length 1 is trivial but they output -1 suggests they require none at all. Actually, they might require that X be a proper substring (maybe not prefix?) that is both a prefix of A and a suffix of reverse(A) and also appears somewhere else? Not.
Thus border still fits tipra case.
But maybe border can be expressed in terms of reverse: A border X (prefix and suffix) of A implies that X is a prefix of A and also X is a suffix of A. If we reverse A, the suffix of A becomes prefix of reverse(A)? Actually reverse of suffix of A is prefix of reverse(A). So if X is suffix of A, then reverse(X) is prefix of reverse(A). For X to be a prefix of reverse(A) as well, we need X = reverse(X), i.e., X palindrome. So not border.
Alternatively, if we consider the concatenation of A and reverse(A), then maybe the border of that concatenation yields something? Not.
Given the time, it's safest to implement border as per sample.
But I'd like to double-check sample: For "abababa", border length 5. Could there be another interpretation that yields 5? For instance, longest common proper prefix of A and reverse(A) that is also a suffix of A? Let's try: Find max k < n such that A[0..k-1] = reverse(A)[0..k-1] and also A[0..k-1] = A[n-k..n-1]. But the first condition A[0..k-1] = reverse(A)[0..k-1] is equivalent to A[0..k-1] = reverse(A[n-k..n-1]), which combined with second gives A[0..k-1] = reverse(A[0..k-1]), so palindrome. So for "abababa", palindrome prefixes lengths 1,3,5,7. Max proper is 5. So again 5. But then for "tipra", palindrome prefixes: "t" length 1 -> proper palindrome prefix exists. So should output 1, not -1. So they must require something more than just palindrome prefix; maybe they also require that this palindrome prefix also appears as a substring elsewhere? Or maybe they require that it's a proper border? Not sure.
Thus border is the only consistent across all three.
But wait, for "abababa", the border length 5, which is also a palindrome? Actually "ababa" is palindrome. So it matches palindrome prefix as well. But border is not necessarily palindrome; for example, consider string "abcab". Border is "ab" length 2, but "ab" is not palindrome. But if sample were "abcab", what would output be? According to border, it would be 2. According to palindrome prefix, it would be 1? Actually palindrome prefixes: "a" (palindrome), "ab" not, "abc" not, "abca" not, "abcab"? Not palindrome. So longest palindrome prefix is 1. So which would the problem likely output? We don't have sample. But many problems ask for longest border, not palindrome prefix. So border is more general.
Given that the story mentions reverse, perhaps the intended is to compute the longest palindromic prefix. That also involves reverse. And the answer for "abababa" would be 5 (if they exclude full string) or 7 (if they include). They excluded full, so 5. For "tipra", longest palindromic prefix is "t" (1). But they output -1. Could it be that they require the palindromic prefix to have length > 1? Then "t" is length 1, so not counted, thus -1. For "manish", "m" length 1, so -1. That would also match sample! Indeed, if the problem is: find the longest proper (i.e., length < n) palindromic prefix, and if none with length > 1, output -1. Then for "abababa", longest proper palindromic prefix is "ababa" length 5. For "tipra", longest proper palindromic prefix is "t" length 1, but they might require length > 1? But then they would output -1? Possibly they consider that a single character prefix is trivial and not considered? But then why would they exclude length 1? Often problems ask for "substring" maybe of length > 1. But they didn't specify. However, the sample output -1 for tipra and manish, which have single-character palindromic prefixes. So indeed they require length at least 2? Or maybe they require that the substring is a proper prefix and also appears as a substring elsewhere? Not.
But the border interpretation for "abcab" would output 2, while palindrome prefix (with min length 2?) would output -1 because "a" is length 1 only. Without additional test case, we can't differentiate.
Let's see constraints: t<10, length up to 100000. Both solutions are O(n). Which is more likely? The mention of reverse strongly suggests palindrome prefix, because reverse is directly used: the second string is reverse of first. If you want to check if a prefix is palindrome, you can compare the prefix with its reverse, which is exactly a prefix of the reverse string. Actually, if we have string S and its reverse R, then a prefix X of S is a palindrome if and only if X equals the prefix of R of same length? Wait: If X is a prefix of S, then X is palindrome iff X = reverse(X). But reverse(X) is exactly the suffix of S of length |X| reversed? However, the prefix of R of length |X| is equal to reverse( suffix of S of length |X| )? Not exactly. Let's check: R = reverse(S). The prefix of R of length k is the first k characters of reverse(S), which are the last k characters of S in reverse order. That is reverse( S[n-k .. n-1] ). So X being palindrome means X = reverse(X). But we don't have X compared to reverse(X) directly via R's prefix unless X is also a suffix of S. Because R's prefix is reverse(suffix). So X = R[0..k-1] would imply X = reverse( suffix of length k ). So X palindrome iff X = reverse(X) but that is not directly X = prefix of R. However, if X is both a prefix of S and a prefix of R, then X = prefix of S and X = prefix of R, so X = prefix of S and X = reverse( suffix of length k ). This condition does not imply palindrome unless we also have that prefix of S equals suffix of S? Not exactly. Let's derive:
Given: X = S[0..k-1] and X = R[0..k-1] = reverse( S[n-k..n-1] ). So we have S[0..k-1] = reverse( S[n-k..n-1] ). This is exactly the condition that the prefix of length k is the reverse of the suffix of length k. That does not imply that S[0..k-1] is palindrome. It implies that the prefix equals the reverse of the suffix. For palindrome, we need prefix equals reverse(prefix). So different.
Thus the condition "X is a prefix of both S and R" is not the same as X being palindrome. It is the condition that the prefix equals the reverse of the suffix. That's a different property. For a string that is palindrome, we have S[0..k-1] = reverse(S[n-k..n-1]) for all k? Actually if S is palindrome, then S = reverse(S), so S[0..k-1] = reverse(S)[0..k-1] = reverse( S[n-k..n-1] ) indeed holds because reverse(S) = S, so prefix of S equals prefix of reverse(S). So for palindrome, the condition holds. But for non-palindrome strings, there might be some k where prefix equals reverse(suffix) but not necessarily palindrome. For example, S = "abcba"? Let's test S="abcba", palindrome, holds. For S="abca", R="acba". Check k=1: prefix "a", R prefix "a", condition holds. k=2: prefix "ab", R prefix "ac": not equal. So no. For S="aabbaa"? Might have some.
But the condition "prefix of both" is exactly: S[0..k-1] = reverse(S)[0..k-1]. That is equivalent to S[0..k-1] = reverse( S[n-k..n-1] ). That's the definition of a "symmetric" condition between prefix and suffix. This is sometimes called a "border" if we also had equality with suffix directly, but here it's with reverse of suffix.
Now, what is the longest k such that S[0..k-1] = reverse(S)[0..k-1]? That's essentially the length of the longest prefix that is equal to the reverse of the suffix of same length. That is known as the longest "reverse prefix-suffix match". This is similar to the concept used in some string algorithms like for building palindrome trees? But it's not the standard border.
Now compute for "abababa": S = "abababa", R = "abababa". So condition holds for all k, so max k = 7. But answer 5, so not that unless proper.
But if they restrict to proper (k < n), then max proper k = 6? Actually all k < n satisfy, so longest proper would be 6. But answer 5. So not that.
What about condition: X is a substring (not necessarily prefix) that is a prefix of both S and R. That means there exists indices i, j such that X = S[i..i+len-1] = prefix of S (so i must be 0) and also X = R[j..j+len-1] = prefix of R (so j=0). So again prefix.
Thus not.
What about condition: X is a substring of S that is also a prefix of R. That is longest prefix of R that occurs as substring in S. For "abababa", R=S, longest prefix of R occurring as substring in S is whole string, length 7. Not 5.
Unless they require X to be proper (less than n) and also not the whole string. Then length 6. Not 5.
Thus border still seems.
Maybe they ask: Given S and reverse(S), find the longest string which is a suffix of S and a prefix of reverse(S). That would be? Suffix of S of length k, and prefix of reverse(S) of length k. Condition: suffix of S = prefix of reverse(S) = reverse(prefix of S). So suffix of S = reverse(prefix of S). For "abababa", suffix of length k = prefix of length k? Actually palindrome, so suffix = reverse(prefix). For k=5, suffix (last 5) = "ababa", prefix = "ababa", reverse(prefix) = "ababa". So holds. For k=6, suffix last 6 = "bababa"? Actually last 6: indices 1-6: b a b a b a = "bababa", prefix 6 = "ababab", reverse(prefix) = "bababa". So holds. For k=7, holds. So longest proper would be 6? Or 6 if exclude full. But answer 5, not 6. So maybe they exclude also length n-1? No.
What if they ask for longest proper prefix of S that is also a suffix of S? That's border, gives 5. But why involve reverse? Possibly because they think that checking suffix of S is same as checking prefix of reverse(S)? Because the reverse of the suffix is prefix of reverse. But they'd need to reverse that string again maybe? Actually, if we want to check if a prefix of S is also a suffix of S, we could take the suffix, reverse it, and see if it matches the prefix of the reverse? Let's see: X is prefix of S. Want X also suffix of S. That means X = suffix. Taking reverse: reverse(X) = reverse(suffix). But reverse(suffix) is prefix of reverse(S). So the condition X = suffix is equivalent to X = reverse( prefix of reverse(S) )? Not straightforward.
However, there is a known technique: To compute borders, one could use the string and its reverse combined with Z-algorithm. For instance, compute Z-array on S + '$' + reverse(S) to find longest prefix of S that appears at the end of S? Something like that. So maybe the problem description intentionally mentions reverse to hint at a solution using reverse concatenation? Possibly the problem is: Given a string, find the longest proper prefix that is also a suffix. But they provide reverse because you could use reverse to solve? Actually, you can solve border with KMP without reverse. So not.
Let's search memory: There is a known problem on HackerEarth or similar: "Arpit and AB de Villiers" maybe? I recall a problem named "Arpit and AB de Villiers" from some contest? Possibly it's about longest palindromic prefix? I'm not sure.
Given the ambiguity, we should look at the input constraints: length up to 100,000. The sample output matches border. The story might be misdirection. The typical problem: "Given a string, find the length of the longest proper prefix which is also a suffix" appears in many coding challenges. And the output format "Case i: ans" is common. I think it's safe.
But before finalizing, let's consider the possibility that the answer is indeed the length of the longest proper border. And for -1 when no border exists (length 0). That's plausible.
I'll implement using KMP prefix function.
Pseudo:
for each test case index i from 1 to T:
s = input string
n = len(s)
pi = [0]*n
for j in range(1,n):
k = pi[j-1]
while k > 0 and s[j] != s[k]:
k = pi[k-1]
if s[j] == s[k]:
k += 1
pi[j] = k
ans = pi[-1]
if ans == 0:
print(f"Case {i}: -1")
else:
print(f"Case {i}: {ans}")
But is there any edge case? For string of length 1, pi[-1]=0, so output -1. But if string is single character, is there any border? No proper prefix that is also suffix (proper prefix must be shorter than string). So -1. That seems fine.
But note: Some definitions of border allow the whole string? Usually "proper" is excluded. So we use proper.
Now, check sample: "abababa" length 7, pi[6] should be 5. Good.
But let's manually compute for "abababa" using above: I did earlier, pi[6]=5. So ans=5.
Now, what about a string like "aaaa"? pi: [0,1,2,3]? Actually for "aaaa", pi[0]=0, pi[1]: s[1]=a, compare with s[0]=a, match, pi[1]=1; pi[2]: compare s[2]=a with s[1]? Actually use method: k=pi[1]=1, s[2] vs s[1]? s[2]=a, s[1]=a match => k=2; pi[2]=2; pi[3]=3. So pi[3]=3. So ans=3. That would be longest proper border. But what about the condition? The longest proper prefix that is also suffix is "aaa" length 3. So output 3. That seems plausible.
But would a problem expecting palindrome prefix output 3 for "aaaa"? Palindrome prefixes: "a", "aa", "aaa", "aaaa". Longest proper is "aaa" length 3. So both would give 3. For "abcab", border gives 2, palindrome prefix gives 1. Without sample, we can't tell. But the story mentions reverse, so maybe palindrome prefix is more relevant because it directly involves the reverse string. Because if you have S and reverse(S), then a prefix X of S is a palindrome iff X equals the prefix of reverse(S) of same length? Wait, not exactly. But we can check: X is palindrome if X equals reverse(X). Reverse(X) is not necessarily the prefix of reverse(S). Reverse(X) is the reverse of the prefix of S. The prefix of reverse(S) of length k is the reverse of the suffix of S of length k. So to check if prefix X is palindrome, you would need to compare it with reverse(X), which is not directly the prefix of reverse(S) unless X is also a suffix. So that doesn't involve reverse(S) directly.
However, if you create a new string by concatenating S and reverse(S) with a separator, you might compute something. Not clear.
Maybe the problem expects to compute the longest substring that is a prefix of S and also a prefix of reverse(S). That's LCP. But sample doesn't match. Unless they misinterpret "substring" as something that appears inside both, not necessarily at start? But prefix forces start.
Wait, maybe they ask: "find the longest substring which is the prefix of both strings" but they consider "both strings" as the original and its reverse. But perhaps they consider "substring" as a string that can be obtained by deleting some characters from the original? No, substring is contiguous.
Given the confusion, I'd like to look up the problem statement maybe from known sources. The sample input and output exactly matches a problem I recall from CodeChef or similar: "Prefix as a Substring" maybe? Or "Longest Prefix Suffix". The sample "abababa" gives 5. That is classic KMP problem. Also the output format "Case i: ans" suggests multiple test cases.
Thus I'm 90% confident it's border.
But to be extra safe, we can think: If it were border, why would the story include reverse? Possibly because Arpit thought AB could do anything; his brother didn't believe; he gave him a problem with two strings, second reverse of first, to find the longest substring which is the prefix of both strings. But maybe he meant "suffix" not "substring". If the problem were: find longest substring which is a suffix of both strings. That's also border? Actually suffix of both strings? For S and reverse(S), suffix of S and suffix of reverse(S). Suffix of S is some ending part; suffix of reverse(S) is reverse of prefix of S. So a string X that is a suffix of both would require X = suffix of S and X = suffix of reverse(S) = reverse(prefix of S). So X = suffix of S and X = reverse(prefix of S). This is not border.
Unless they meant "longest substring which is a prefix of the first and a suffix of the second". That we considered yields palindrome prefix.
Maybe they meant "longest substring which is a prefix of the first and a prefix of the second's reverse"? That's nonsense.
Given that the sample fits border so perfectly, I'll go with that.
Thus solution: compute LPS (longest prefix suffix) array using KMP, output last value or -1.
Implementation details: We'll read T, then loop.
Edge Cases: large strings up to 1e5, so O(n) per test is fine. Use Python with efficient loops.
We must be careful about Python recursion? Not needed.
We'll output as required.
Now, I'll produce final answer: Python code.
But maybe there is nuance: The answer should be the length of the longest substring (maybe proper) that is prefix of both strings. For string that is palindrome, the whole string is prefix of both, but they output 5 for abababa, so they exclude the whole string. So we need to output length of longest proper prefix that is also a prefix of reverse? That would be length n-1? For abababa, n-1=6, but output 5. So not.
Thus border.
Thus I'll implement.
One more check: For "abababa", border length 5. If we consider the longest proper prefix that is also a prefix of reverse, it's 6? Because prefix of length 6 of original is "ababab", and prefix of reverse (same) is "ababab", so match. So length 6 would be longer. So why 5? Unless they consider "substring" not "prefix"? If they consider substring that appears anywhere, maybe they want the longest substring that appears as a prefix in both strings but also appears elsewhere? For "abababa", the substring "ababa" appears as prefix (length 5) and also appears elsewhere? Actually "ababa" appears at indices 0 and 2? At index 2 it's "ababa"? Indices 2-6: "ababa". So it appears as substring at position 2 as well. But the whole string appears only at index 0. Maybe they want the longest substring that is a prefix of both strings and also appears as a substring (maybe not necessarily at the start) in both? But being a prefix of both already implies it appears at start. The additional condition could be that it also appears somewhere else (i.e., at least one more occurrence) in the original string. That would be the longest border that is not the entire string. For "abababa", the longest proper border is "ababa" length 5, which appears at start and at index 2. So qualifies. The whole string length 7 is a prefix but maybe it does not appear elsewhere? Actually the whole string appears only at index 0, not elsewhere. So if they require the substring to appear at least twice (maybe as a prefix and also somewhere else), then the answer would be the longest border. That is a known problem: "Longest prefix that also occurs as a substring elsewhere in the string". That's different from border (which is prefix that equals suffix). For "abababa", prefix "ababab" length 6 appears elsewhere? Does "ababab" occur at another position? Substrings of length 6: indices 0-5: "ababab", indices 1-6: "bababa". So "ababab" does not occur at index 1. So only at index 0. So not elsewhere. Prefix "ababa" length 5 occurs at index 0 and index 2. So longest prefix that appears elsewhere is length 5. That matches sample! And for "tipra", prefix "t" length 1 appears elsewhere? Does "t" appear elsewhere in "tipra"? It appears only at beginning? "t" appears at index 0. Are there other 't's? The string is "t i p r a". Only one 't'. So does not appear elsewhere. So longest prefix that appears elsewhere is length 0 -> -1. For "manish": 'm' appears only once, so -1. This interpretation fits the sample as well!
Now, how does this relate to the problem statement? "He gave him two strings where the second string was the reverse of the first. He asked him to find the longest substring which is the prefix of both strings." Hmm, that doesn't mention appearing elsewhere. But maybe the intention is: Since the second string is the reverse, a prefix of the original that also appears as a prefix of the reverse? Wait, a prefix of the reverse corresponds to a reversed suffix of the original. So maybe they want a string that is a prefix of the original and also a prefix of the reverse, i.e., appears at the beginning of both. But that's LCP, not border.
But consider the "appears elsewhere" condition: It is not stated.
However, there is a known problem: "Given a string, find the longest substring which is a prefix, a suffix and also appears elsewhere in the string." That's the "border that occurs at least three times"? Actually that's the "property of strings" like in problems like "Find the longest proper prefix of S that is also a suffix and also occurs as a substring in S at least one more time." This is often solved using prefix function and counting occurrences. But sample only has one test with border 5 which also appears elsewhere. For "abababa", border 5 appears elsewhere, but border 5 is the longest proper border. Also border 3 appears elsewhere. But the longest proper border (5) also appears elsewhere. So it's consistent with "longest border" as well, because any border of a string automatically appears as a prefix and also as a suffix. But does a border necessarily appear elsewhere? It appears at least twice: as prefix and as suffix. For non-overlapping? For "aaaa", border length 3 appears as prefix and suffix; it also appears elsewhere? It appears many times. So border ensures at least two occurrences. But the requirement "prefix of both strings" would be two occurrences: one as prefix of first, one as prefix of second. But that's two different strings, not two occurrences in same string. So maybe they consider two strings: original and reverse. A substring that is a prefix of both strings would be a string that appears as a prefix of original and as a prefix of reverse. That is two occurrences: one at start of original, one at start of reverse. That is exactly what we considered as LCP. But for "abababa", that gives 7, not 5. Unless we exclude the case where the prefix is the whole string? If we exclude whole string, we get 6, not 5. So not.
But if we consider that the substring must be a prefix of the original and also a prefix of the reverse, but also must be a substring (i.e., appear somewhere) in the original? Actually the phrase "longest substring which is the prefix of both strings" might be parsed as: Among all substrings (of any string? Possibly of the original string), find one that is a prefix of both given strings. That means we look at substrings of the original string (or maybe of both) and test if they are prefixes of both strings. A substring of the original that is a prefix of both strings must be a prefix of the original (since it's a substring of the original that equals a prefix of the original? Not necessarily: a substring of the original could be equal to the prefix of the original but not necessarily starting at index 0. For example, in "abababa", the substring starting at index 2 of length 5 is "ababa", which equals the prefix of length 5. So it is a substring (not necessarily at start) that matches the prefix. And it also must be a prefix of the reverse. So we need to find a substring X in A (could be anywhere) such that X = prefix of A (i.e., the first |X| characters of A) and also X = prefix of reverse(A). So we need to find a substring of A that matches the prefix of A, and also matches the prefix of reverse(A). The substring itself does not have to be located at the beginning of A, only its content must equal the prefix of A. So essentially, we are looking for occurrences of the prefix of A within A (including possibly at position 0) that also satisfy that this string is a prefix of reverse(A). So we need the longest length L such that the prefix of A of length L appears as a substring somewhere in A (maybe multiple times) and also this string is a prefix of reverse(A). But if it appears as a substring somewhere, it's automatically a prefix of A? Not exactly; we require that the substring equals the prefix of A of length L. So it's an occurrence of the prefix. So we need to find the longest L (1 <= L <= n) such that the prefix of A of length L appears at least once in A (maybe at position 0 is guaranteed) and also this prefix is also a prefix of reverse(A). But note that the prefix of A always appears at position 0, so the condition of appearing somewhere is trivial (at least one occurrence). But perhaps they require that the substring appears as a prefix of both strings and also appears as a substring somewhere else? Not stated.
Actually, the phrase "longest substring which is the prefix of both strings" could be interpreted as: we have two strings S and R. We want a string X that is a substring of ... something? Usually, when we say "substring which is the prefix of both strings", we mean that X is a substring (of something unspecified) and also X is a prefix of S and a prefix of R. Without specifying which string X is a substring of, it could be that X is a substring of S (or R) or both. Typically, one says "Find the longest common substring that is also a prefix of both strings". That means among all substrings that are common to S and R, pick those that are prefixes of both S and R, and take the longest. That's LCP of S and R, because any common substring that is a prefix of both must start at index 0 in both, so it's a common prefix. So that's LCP. So I'm back to LCP.
But sample contradicts.
Given the new interpretation: longest prefix of S that appears as a substring elsewhere in S and also is a prefix of R. For "abababa", prefixes that are also prefixes of R? Since R = S, all prefixes are prefixes of R. So we need the longest prefix that appears elsewhere in S. That's the longest proper border? Actually, the longest prefix that appears elsewhere in S is the longest L such that the prefix of length L occurs at some index i>0. This is known as the longest "border" that is not overlapping? Actually, a border ensures occurrence at the end, not necessarily elsewhere. For "abababa", prefix of length 5 occurs at index 2 (i=2). Prefix of length 6 does not occur elsewhere (only at index 0). So the longest prefix occurring elsewhere is length 5. So that matches sample. For "tipra", prefix of length 1 't' appears at index 0 only? Does it appear elsewhere? There is only one 't', at index 0. So no other occurrence. So longest such is 0 -> -1. For "manish", similarly. So this interpretation fits sample, and also uses the reverse? Not yet. Why involve reverse? Because we also require that the string is a prefix of reverse(A). In this interpretation, we additionally require that the prefix of A is also a prefix of reverse(A). For "abababa", since it's palindrome, all prefixes satisfy. For "tipra", we need to check if the prefix of A appears as a prefix of reverse(A) as well. For "tipra", reverse = "arpit". The prefix of A of length 1 is "t". Is "t" a prefix of "arpit"? No, because the first character of reverse is 'a'. So even if "t" appeared elsewhere, it would fail the condition of being a prefix of reverse. So that would also give -1. For "manish", prefix "m" vs reverse "hsinam": first char 'h', not match. So -1. For "abababa", all prefixes are also prefixes of reverse (since palindrome), so the limiting factor is the occurrence elsewhere. So answer is longest prefix occurring elsewhere. That yields 5.
Now, could there be a string where a prefix appears elsewhere but is not a prefix of reverse? Then answer might be lower. That still fits the condition: we need the substring to be a prefix of both strings (original and reverse). So it must satisfy both. So essentially, we are looking for the longest prefix of the original that also is a prefix of the reverse and also appears as a substring (maybe at any position) in the original? But why specify substring? The phrase "longest substring which is the prefix of both strings" might be interpreted as: find the longest string that is a substring (of the original? or of the reverse?) and also is a prefix of both strings. If we take the original as the main string, then we are looking for substrings of the original that happen to be prefixes of both the original and its reverse. But a substring of the original that is a prefix of the original must start at index 0? Not necessarily: Consider substring at index i, it might be equal to the prefix of the original even if i>0. So it's possible that a substring (starting at some i>0) has the same content as the prefix of the original. So that substring is a prefix of the original (content-wise) but not located at the start. And it also must be a prefix of the reverse. So indeed we want the longest string X such that X appears as a substring of the original (at any position) and X is equal to the first |X| characters of the original (i.e., a prefix) and also equal to the first |X| characters of the reverse. That is exactly: X is a prefix of original, a prefix of reverse, and occurs somewhere in original (maybe at position 0 automatically, but also might occur elsewhere; but the "substring" condition is that X is a substring of the original; it is automatically because any prefix is a substring (starting at 0). So the condition "substring" is redundant if we allow position 0. But perhaps they intend that the substring must be a proper substring (i.e., not necessarily starting at index 0), meaning it can be found somewhere else? Not necessarily. Usually "substring" just means a contiguous part of a string; it can be at any position, including start. So it includes the prefix as a substring. So if we say "find the longest substring that is a prefix of both strings", the substring itself is the string we are looking for; it doesn't need to be a substring of anything else; it is just a string. The phrase "which is the prefix of both strings" describes the property of that substring. It doesn't say "substring of the given strings". It says "substring which is the prefix of both strings". Typically, one would say "common prefix" not "substring". So I'm leaning that the intended meaning is "find the longest string that is both a prefix of the first string and a prefix of the second string". That is the common prefix. But sample says 5, not 7.
Unless they implicitly consider that the substring must also be a suffix? Hmm.
Let's search online memory: I recall a problem named "AB and the Prefix" maybe. But given the sample, I think the correct interpretation is the border (longest proper prefix that is also a suffix) because it's a classic. Many judges have such a problem. And the story might be just a fun context.
But the new interpretation (longest prefix that appears elsewhere and also is prefix of reverse) is more complex and less likely to be asked without clear statement.
Also, the phrase "longest substring which is the prefix of both strings" could be read as: "longest substring of the first string that is also a prefix of the second string". That is: find the longest substring of A that is a prefix of reverse(A). That would be: find the longest prefix of reverse(A) that appears as a substring in A. That's another known problem: compute the longest prefix of reverse that occurs in A. That is essentially the longest palindromic prefix? Let's check: The longest prefix of reverse(A) that appears in A. For A="abababa", reverse(A)="abababa". Its prefixes are "a","ab",..., "abababa". All appear in A (since A is same). So the longest is length 7. So answer would be 7, not 5.
What if we restrict to proper substring? Still 7? The whole string is a substring of A (at index 0). So 7.
Thus no.
Given the constraints and sample, I'm comfortable with border.
Thus final solution: compute LPS.
Implementation:
def longest_prefix_suffix(s):
n = len(s)
lps = [0]*n
length = 0
i = 1
while i < n:
if s[i] == s[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length-1]
else:
lps[i] = 0
i += 1
return lps[-1]
But careful: The standard algorithm computes lps array. We'll use that.
Alternatively, we could use Python's iterative method.
Edge Cases: For string length 0? Not possible as per constraints (length >=1). For string length 1, lps[-1]=0 -> -1.
Now, we need to output "Case i: ans" with i starting from 1.
Now, let's test with given samples:
"abababa": lps[last] =? I'll simulate quickly:
s = "a b a b a b a"
lps[0]=0
i=1, char b, length=0, s[1]!=s[0] => length=0, lps[1]=0, i=2
i=2, char a, length=0, s[2]==s[0] => length=1, lps[2]=1, i=3
i=3, char b, length=1, s[3]==s[1]? s[3]=b, s[1]=b, yes => length=2, lps[3]=2, i=4
i=4, char a, length=2, s[4]==s[2]? a==a yes => length=3, lps[4]=3, i=5
i=5, char b, length=3, s[5]==s[3]? b==b yes => length=4, lps[5]=4, i=6
i=6, char a, length=4, s[6]==s[4]? a==a yes => length=5, lps[6]=5, i=7 end.
Thus ans=5.
"tipra": simulate? It will yield 0.
"manish": 0.
Thus matches.
Now, confirm that output format exactly: "Case 1: 5" etc.
Thus code.
But before final, think about any pitfalls: The problem might require using 0-based indexing? no.
Also, we need to handle multiple test cases and large input, so we should use sys.stdin.read() or sys.stdin.readline for speed.
I'll implement:
import sys
def compute_lps(s):
n = len(s)
lps = [0]*n
length = 0
i = 1
while i < n:
if s[i] == s[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length-1]
else:
lps[i] = 0
i += 1
return lps[-1]
def main():
import sys
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
out_lines = []
idx = 1
for case in range(1, t+1):
s = data[idx]
idx += 1
ans = compute_lps(s)
if ans == 0:
out_lines.append(f"Case {case}: -1")
else:
out_lines.append(f"Case {case}: {ans}")
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
This should work.
But let's consider if there is any possibility that they want the longest common prefix (LCP) but proper, and for "abababa" they output 5 because maybe they consider that the prefix must be also a suffix? I'm still uneasy about the mention of reverse. Could there be a scenario where LCP yields 5? Only if the reverse is not exactly the same because they might be considering the second string as the reverse but then maybe they are concatenated? No.
Perhaps the problem is: Given two strings (original and its reverse), find the longest substring that appears as a prefix in the original and as a prefix in the reverse. That is LCP. For "abababa", that's 7. Unless they consider that the substring must be also a proper substring of the original not equal to the whole string? Then it's 6. So no.
What about if they consider the substring must be also a suffix of the original? That would be border. That yields 5. But why mention reverse? If we want a prefix that is also a suffix, we don't need reverse. Unless the brother gave two strings: original and reverse, and asked for the longest substring that is a prefix of both. That condition, prefix of reverse, is equivalent to suffix of original reversed? Not exactly. But if a string is a prefix of both original and reverse, then it is a prefix of original and a prefix of reverse. That doesn't imply it's a suffix of original. However, if the original is palindrome, it holds. But not in general.
Maybe the test case "abababa" is purposely a palindrome, so they got 5. But they also have non-palindromic cases that yield -1. For a non-palindromic string, could there be a non-zero length prefix that is also a prefix of reverse? For any string, compare first character: if first char of original equals first char of reverse, that means original[0] == original[-1], i.e., the string starts and ends with same character. So there could be a match of length 1 if first and last character are same. For "tipra", 't' vs 'a' not same, so length 0. For "manish", 'm' vs 'h' not same. So -1. So if they output -1, that matches LCP=0. For "abababa", LCP=7, but they output 5. So why not 7? Possibly because they require the substring to be proper (not the whole string) and also require that it is not the whole string and maybe also that it appears as a substring elsewhere? But that is not stated.
Wait, if they require proper (k < n) and also maybe they require that the substring is a proper substring of the original, meaning it must appear somewhere else? Actually "substring" could be any contiguous part. The phrase "longest substring which is the prefix of both strings" can be interpreted as: among all substrings of the original string (or maybe among all substrings of the two strings) that have the property that they are a prefix of the first string and also a prefix of the second string. A substring of the original that is a prefix of the original must start at index 0. So it is exactly a prefix. So the set of such substrings is just all prefixes of the original that are also prefixes of the reverse. So it's LCP again.
Thus we circle back.
Given the sample mismatch, either the sample is from a different problem or my interpretation is off.
Maybe the problem expects the length of the longest palindromic substring that is also a prefix? That would be the longest palindrome that starts at index 0. For "abababa", that's 7. Not 5.
Maybe it's the longest substring that is both a prefix and a suffix (border) but the story mentions reverse because the second string is reverse of the first, and a suffix of the first corresponds to a prefix of the reverse. Because if a string is a suffix of the first, its reverse is a prefix of the reverse. However, if we want a string that is a prefix of both, we could consider the original and its reverse: a string that is a prefix of original and a prefix of reverse is equivalent to a string that is a prefix of original and the reverse of a suffix of original? Not border.
But if we take the string that is a border (prefix and suffix) and then reverse it, it becomes a prefix of the reverse and a suffix of the reverse? Hmm.
Maybe the problem is actually: "find the longest substring which is a prefix of the first string and a suffix of the second string". Because the second is reverse, this becomes: prefix of S and suffix of reverse(S). Suffix of reverse(S) is reverse of prefix of S. So the condition becomes prefix of S is palindrome. That's palindrome prefix. For "abababa", palindrome prefix longest proper is 5. For "tipra", palindrome prefix length 1. But they output -1. So if they require length > 1, then -1 for tipra. That matches if they consider a palindrome prefix of length at least 2. However, "manish" also has palindrome prefix length 1, so -1. So that would also match sample if they ignore length 1. So we have another interpretation that matches sample if we add constraint that substring length must be >1? But the problem statement didn't mention that. But sometimes problems consider non-empty? It says "substring", could be of length >=1. So they should output 1 for tipra if palindrome prefix. So they didn't.
But maybe they consider that the substring must be "proper" in the sense of not trivial? Unlikely.
Thus border still stands because it yields 0 for tipra.
Let's also test a hypothetical case: Suppose input is "aa". Border length? lps[1]=1? Actually "aa": lps[0]=0, i=1: s[1]=a, length=0, s[1]==s[0] => length=1, lps[1]=1. So ans=1. Output would be 1. If they expected palindrome prefix (with min length 1) they'd also output 1. So not distinguishing.
Consider "abca". Border? Check: "abca": any border? prefix "a", suffix "a" => border length 1. So ans=1. Palindrome prefix? "a" length 1, so 1. So both give 1.
Consider "abcab". Border: "ab" length 2. Palindrome prefix: "a" length 1. So these differ. Which would be the answer according to the problem? Without a test case, we can't know. But the sample didn't include such case. The story mentions reverse, which suggests a connection to palindrome. In palindrome prefix scenario, we only need the original and its reverse, and we can compare prefix of original with prefix of reverse? But as we argued, that gives LCP, not palindrome. To check palindrome prefix, you compare prefix of original with reverse of prefix (or compare prefix with suffix of reverse? Actually, if we have reverse string, then to check if prefix of length k is palindrome, we need to compare prefix of original with reverse of prefix of original. But the prefix of reverse of length k is reverse of suffix of length k. So not directly.
But maybe they define the problem as: Given S and reverse(S), find longest string X such that X is a substring of S and also a prefix of reverse(S). This is the longest prefix of reverse that occurs as substring in S. That is a classic problem (sometimes called "Find the longest prefix of the reversed string that appears in the original"). For "abababa", reverse prefix of length 7 appears in S at position 0, so answer 7. For "tipra", reverse="arpit", its prefixes: "a", "ar", "arp", "arpi", "arpit". Do they appear as substrings in "tipra"? "a" appears at index? "tipra" contains 'a' at last index? Actually "tipra" has 'a' at index 4. So "a" appears, so length 1. "ar" appears? "ti p r a": substring "ar"? No. So longest is 1. So output would be 1, not -1. So not.
What if we require that the substring be a prefix of both strings? That's LCP.
Thus border remains the only consistent with sample where tipra gives -1.
But could border be explained with reverse? Let's try: If we take the original S and reverse R, and we look for a substring that is a prefix of S and also a prefix of R. That's LCP. For S="abca", LCP? S="abca", R="acba". Compare: first char 'a' = 'a'; second char 'b' vs 'c' no, so LCP length=1. Border length=1 as well. So matches. For S="abcab", LCP? S="abcab", R="bacba". Compare: 'a' vs 'b' no -> 0. Border=2. So they differ. Which one would the problem output? We don't know. But the sample includes only cases where LCP equals border? For "abababa", LCP=7, border=5, they chose 5. For "tipra", LCP=0, border=0, both 0. For "manish", LCP=0, border=0. So sample does not differentiate between LCP and border except for the palindrome case where LCP > border. They output border. So it's likely border.
Therefore, I'll go with border.
Now, before final, let's see if there's any catch: The problem says "if no such substring can be found in the given string then output -1." That phrasing "in the given string" suggests that the substring must be found in the given string. That is, we are looking for a substring of the given string that satisfies the property. So "substring" is a substring of the given string. And we want it to be a prefix of both strings. So we need to find a substring of A that is a prefix of A and a prefix of reverse(A). Since it's a substring of A, it must appear somewhere in A. If we pick the prefix itself (starting at 0), it is a substring. So it qualifies. So the longest such would be the longest prefix that is also a prefix of reverse. That is LCP. Again we get LCP. So why would they output 5 for "abababa"? Maybe they require that the substring appears somewhere else, not at the start? The phrase "in the given string" might be emphasizing that the substring must be present in the given string. But any prefix is present. So unclear.
Maybe they mean "substring" as a string that can be formed by deleting some characters (non-contiguous)? No.
I think the intended is border.
Given the above, I'll produce final answer as code for border.
But I want to be absolutely sure. Let's search for similar problems: I recall a problem "Longest Prefix Suffix" on GeeksforGeeks. Sample: Input: "ababab", Output: 4 (since border "abab"? Actually "ababab" border length 4? Let's check: "ababab": prefix "abab" = suffix "abab"? Suffix of length4: indices 2-5: a b a b? That's "abab". So border 4. So if input was "ababab", output would be 4. For "aaaa", output 3. So typical.
Now, the sample in our problem is "abababa" output 5. That's consistent.
Thus I'm comfortable.
I'll provide solution using KMP (LPS). Also note that there is a Z-function approach: Compute Z for s + '#' + reverse(s) etc., but not needed.
Given constraints up to 100k, Python solution should be fine with O(n) using while loops.
Edge Cases: All characters same: answer = n-1. Because lps[n-1] = n-1? Actually for "aaaa", n=4, lps[3]=3. So answer 3. That's proper border.
String length 1: answer 0 => -1.
Now, confirm output format exactly: "Case 1: 5". There is a space after colon? Sample shows space. So f"Case {case}: {ans}" is correct.
Now, produce final answer. ```python
import sys
def longest_proper_prefix_suffix(s: str) -> int:
"""Return the length of the longest proper prefix of s that is also a suffix."""
n = len(s)
lps = [0] * n
length = 0 # length of the previous longest prefix suffix
i = 1
while i < n:
if s[i] == s[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
# fall back to the next best candidate
length = lps[length - 1]
else:
lps[i] = 0
i += 1
return lps[-1]
def main() -> None:
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for case_no in range(1, t + 1):
s = data[idx]
idx += 1
ans = longest_proper_prefix_suffix(s)
if ans == 0:
out.append(f"Case {case_no}: -1")
else:
out.append(f"Case {case_no}: {ans}")
sys.stdout.write("\n".join(out))
if name == "main":
main()