We need to solve: given two strings A and B (lowercase alphabets), can we choose non-empty substrings s1 (from A) and s2 (from B) such that concatenation s1 + s2 is a palindrome?
We need to answer Yes or No for each test case.
Constraints: |A|,|B| ≤ 1000, T ≤ 10, so total length up to 10k, maybe O(n^2) per test is acceptable (1e6). But we need to be careful: if we try all substrings of A and B, that's O(n^3) worst-case? Actually number of substrings for each is O(n^2). So checking all pairs of substrings would be O(n^4) which is too much. We need smarter approach.
We need to check if there exists s1 (substring of A), s2 (substring of B) such that s1 + s2 is palindrome.
Let x = s1, y = s2. Then concatenated string p = x y is palindrome.
Let len = |x|+|y|. Palindrome condition: for all i from 0 to len-1, p[i] = p[len-1-i].
We can think of the concatenated palindrome as composed of two parts from A and B. Since s1 is from A, s2 from B. The characters in the palindrome come from A and B.
One approach: Consider all possible palindromic strings that can be formed by concatenating a substring from A and a substring from B. Equivalent to: There exists indices i ≤ j for A (s1 = A[i..j]) and k ≤ l for B (s2 = B[k..l]) such that A[i..j] + B[k..l] is palindrome.
Alternatively, we can think of the palindrome condition: The string can be split at some point between the two parts. Let the palindrome have left half and right half. Since concatenation is x then y, the split between x and y is somewhere inside the palindrome. The boundary may be anywhere, even inside a character? The split is between the two substrings, so the palindrome's first |x| characters come from A, and the last |y| come from B.
Let m = |x|, n = |y|.
Then palindrome condition: for all i in [0, m-1], x[i] must equal something; similarly for y.
Specifically, for i from 0 to m-1, x[i] must equal the character at position (m+n-1-i) in the concatenated string. That position could be in x (if m+n-1-i < m) or in y (if >= m). Similarly for positions in y.
So, the palindrome can be symmetric in many ways. For example, if x is long enough to cover the whole first half, etc.
We can try to think of checking if there is any common character between A and B? Because a simplest palindrome could be a single character from A and a single character from B? But s1 and s2 both must be non-empty. So we need at least one character from each. Could we have palindrome of length 2? That would be two characters: first from A, second from B. It is palindrome if they are equal. So if there exists a character c such that A contains c as a substring (any single character substring) and B contains c as a substring, then we can choose s1 = "c", s2 = "c" => concatenation "cc" palindrome. Actually s1 = "c" (non-empty substring of A), s2 = "c" (non-empty substring of B), then s1+s2 = "cc", which is palindrome. So if A and B share any common character, answer is Yes.
But wait, is that always? If they share a character, we can pick that character from each. Yes, that yields length 2 palindrome. However, what if the palindrome length is 1? That would require s1+s2 of length 1, but both s1 and s2 must be non-empty, so min length is 2. So length 2 palindrome requires two equal characters. So indeed if there is any common character, answer is Yes.
But is there any scenario where there is no common character yet a longer palindrome exists? Possibly yes. For example, A = "ab", B = "cd". No common character, can we form a palindrome? Suppose s1 = "ab", s2 = "ba"? But B does not have "ba". B has "c" and "d". So no. Could there be a palindrome like "abcba"? That would require A substring and B substring such that concatenation is palindrome. If A and B have disjoint character sets, maybe still possible if the palindrome uses multiple characters? For palindrome, the characters must match across the axis. If A has characters from set S_A, B from S_B, then the concatenated palindrome would have pairs of characters that must be equal. So any character from A that appears in the left part must match either a character from A in the right part (if the right part includes some of A? But right part is only from B because concatenation is A then B; the rightmost part is B. However, the matching position for a character in A left part could be in A (if the mirrored position is also in A) or in B (if mirrored position is in B). Similarly, characters in B must match characters in A or B.
But note: The entire string is x (from A) then y (from B). So positions 0..m-1 are from A; positions m..m+n-1 are from B. For a position i in A (0 ≤ i < m), the mirrored position j = (m+n-1-i). If j ≥ m, then it falls in B; else if j < m, it falls in A (i.e., both i and j are in A). Similarly for a position i in B (m ≤ i < m+n), its mirrored j is in A if j < m, else in B.
Thus the palindrome can be decomposed into overlapping conditions:
- For i in [0, m-1] and j = m+n-1-i:
- If i <= j (but careful: i < j? Actually if i = j then the character is middle and it matches trivially; but since m+n could be even or odd, there is a middle position when length is odd. The condition holds automatically as the same character matches itself. But if i and j are both from A or one from A one from B, we need equality.)
We can think of the palindrome as consisting of two parts: the part of x that is matched with part of y, and the part of x matched with itself (if x is long enough to cover the center), and similarly y matched with itself.
Specifically, the palindrome condition implies that if we reverse y, it must match the beginning of x appropriately, and the suffix of x must be mirror of prefix of y? Let's formalize.
Let x = s1, y = s2. Then palindrome means x y = reverse(x y) = reverse(y) reverse(x). So x y = reverse(y) reverse(x). This is a string equation. But since x and y are from different sources, it's not directly decoupled.
Better: The concatenated string p = x y is palindrome => p = reverse(p) => x y = reverse(y) reverse(x). This suggests that x is a prefix of reverse(y) reverse(x) and reverse(y) is a prefix of x? Not exactly.
Alternatively, we can think of the condition in terms of matching characters from the ends inward. The leftmost character (x[0]) must equal the rightmost character (y[n-1] if n>0, else? n>0 always). Actually rightmost character is from B's last character (since y is at the end). So condition: x[0] = y[n-1]. Next: if m>1 or n>1, second character from left: if m>1 it's x[1], else if m=1 then it's y[0]? But careful: The second character from left could be from x if m>=2, else if m=1, then the second character from left is y[0] (since x occupies only first char). Similarly, the second from right is from B if n>=2 else from A. So the constraints can be expressed as: There exists some split such that when we align the strings, the characters match accordingly.
We can try to reduce to checking for existence of a palindrome with the boundary between A and B somewhere. Equivalent to: There exists an index i (0 <= i <= m+n-1) representing the boundary? Actually we don't know lengths.
Another approach: Since |A|,|B| <= 1000, we could try all possible substrings s1 of A (O(|A|^2) = up to 1e6) and for each, check if there exists a substring s2 of B such that s1+s2 is palindrome. But then for each s1 we need to check existence of s2 efficiently. Possibly we can precompute something about B, like all substrings reversed, or use palindromic conditions to determine required s2.
Given a fixed x = A[i..j] of length m, we need to find any y = B[k..l] (non-empty) such that x+y is palindrome. This is like: find y such that y = reverse of some prefix of reverse(x)??? Let's derive.
We have p = x y palindrome => p = reverse(p) => x y = reverse(y) reverse(x). Let rev(x) denote reverse of x, rev(y) similarly.
So x y = rev(y) rev(x). This is an equation between strings.
Let’s denote that the concatenated string is symmetric. Think of aligning x and rev(y) etc.
We can also think of constructing y from x. Suppose we attempt to extend x on the right with y to form a palindrome. For a given string x, the minimal palindrome that has x as prefix is obtained by appending the reverse of x's appropriate suffix. Typically, to make a palindrome starting with x, you can append the reverse of x's prefix to complete it. But here we need the suffix to be exactly a substring from B. So for each x, we can compute the required suffix (call it z) such that x z is palindrome. But z may be longer than x or shorter? Actually, if we want x to be the first part of a palindrome, then the full palindrome must be x + something (call it w) such that total is palindrome. The required w is determined by x: w must be such that x w = palindrome. This means w must equal reverse of some suffix of x? Let's recall: For a given prefix x, the palindrome completion on the right is: find the longest suffix of x that is a palindrome prefix? Actually, suppose we have a string p = x + y that is palindrome. Then the reverse of p is p itself: p = reverse(x) + reverse(y)? Wait reverse(p) = reverse(y) + reverse(x). So p = reverse(y) + reverse(x). But p also = x + y. So x + y = reverse(y) + reverse(x). This suggests that x and reverse(y) overlap. This is similar to the condition that x is a prefix of reverse(y) + reverse(x) and reverse(y) is a prefix of x + y. Not straightforward.
Better: Consider the palindrome condition char by char from both ends. Let’s denote L = 0 (left), R = m+n-1 (right). While L < R, compare char at L and R. The left side is from A until we cross the boundary, the right side is from B until we cross the boundary. This is like a two-pointer match crossing the boundary.
Alternatively, we can think of the palindrome as being formed by taking some overlapping region between the end of x and the start of reverse(y). Actually, from the equation x y = rev(y) rev(x), we see that x must equal some prefix of rev(y) rev(x). This implies that rev(y) must be a suffix of x? Let's derive systematically.
Let |x| = m, |y| = n. Write x = x1 x2 ... xm, y = y1 y2 ... yn.
Palindrome condition: For all i from 1 to m+n, character at position i (1-indexed) equals character at position m+n+1-i.
We can separate cases based on whether the mirrored positions lie in same string or different.
Case 1: Positions both in x (i and j both <= m). This would require that within x, some characters match each other. That imposes that x itself must be palindromic in its overlapping region.
Case 2: Positions one in x, one in y. This couples characters from A and B.
Case 3: Positions both in y.
The palindrome can be seen as: there exists a center, and the strings are reflected.
One systematic way: The concatenated string is palindrome => there exists a non-negative integer t such that the string can be partitioned as:
Let the palindrome length L = m+n.
Define the "midpoint" between characters for even length, or at a character for odd. It might be easier to think in terms of two cases: even length and odd length.
But maybe we can reduce the problem to checking if there exists any character that appears in both A and B (as we already thought) OR there exists some longer palindrome where the two strings interlock such that the characters from A and B match in a cross way. However, if A and B have no common characters, could a palindrome exist? Let's examine.
Suppose A and B have disjoint character sets: no character appears in both. For a palindrome, the leftmost char (from A) must equal the rightmost char (from B's last character). Therefore, the first character of s1 must equal the last character of s2. So that character must belong to both A and B (since first char of s1 is a character from A; last char of s2 is a character from B). Hence they must have a common character. Wait: is that always? Let's check: s1 is substring of A, s2 substring of B. So the first character of the palindrome is the first character of s1, which is from A. The last character is the last character of s2, which is from B. For palindrome, these two must be equal. So indeed, there must exist at least one character that appears in both A and B. That seems necessary.
But is it necessary? Could there be a scenario where the palindrome length is odd and the first and last are not directly compared because the center is something else? Actually, for any palindrome of length ≥ 2, the first and last characters are compared and must be equal. That's always true. So first char from A, last char from B => they must be equal. So indeed, a common character is necessary.
But what about length 1? That would require m+n=1, but both substrings must be non-empty, so minimum total length is 2. So yes, necessary condition: there exists at least one character that appears in both A and B. However, is that also sufficient? We already gave example: choose s1 = that character from A, s2 = that character from B => palindrome of length 2. So the condition is both necessary and sufficient? Let's double-check.
Consider that we need both substrings non-empty. So we can always take a single character from each, provided that character is present in both strings. That yields "cc", which is palindrome. So indeed, if there is any common character, answer is Yes.
But wait: Are we allowed to take a single character as substring? Yes, a substring is defined as a contiguous sequence of characters; a single character is a valid substring.
Thus, it seems the problem reduces to checking whether A and B share at least one common character. But is there any catch? Could there be any scenario where they share a common character but we cannot pick a substring because the common character might need to be from a specific position? No, any occurrence of that character in A and any occurrence in B qualifies. So yes.
But is it always possible to pick s1 from A and s2 from B such that s1+s2 is palindrome? We gave "cc". That works. So answer Yes.
But what about the case where they don't share any common character? Could there still be a palindrome? Let's attempt to construct a counterexample: A = "a", B = "b". No common char, can't have palindrome because first char 'a' vs last char 'b' mismatch. So No.
What about A = "ab", B = "ba"? They share 'a' and 'b', so yes. But consider A = "ab", B = "cd". No common, seems impossible.
But could there be a longer palindrome where the first and last characters are not simply the first char of s1 and last char of s2? Actually they are always. So must match. So necessary condition: first char of s1 equals last char of s2. But is it necessary that this character appears in both strings? Yes, because it's a character from A and from B. So common character is necessary.
Thus the problem appears trivial. But why would the example have test cases like "abba" and "baab"? They share many characters, so Yes. And test 2: "a" and "b" -> No common -> No. Test1: "abc" and "abc" share a,b,c -> Yes. So matches.
But maybe there's a twist: The substrings s1 and s2 must be non-empty, but they could be of length >1. However, the necessary condition still holds.
I recall that there is a known CodeChef problem "Palindromic Substrings" or something? But this problem statement might be from CodeChef (Chef and Palindromes). I'm trying to recall if there is any trick: Could there be a scenario where A and B have common characters, but we cannot form a palindrome because we need to also satisfy the inner characters? But we are only required to form one palindrome from some substrings, not all. So we can choose the simplest: s1 = single char c, s2 = single char c. That gives palindrome "cc". So it's always possible if c in both.
Thus answer Yes iff there exists at least one character that appears in both A and B.
But wait, is there any constraint that s1 and s2 must be non-empty and also that they are substrings (contiguous). Yes, single character is contiguous.
Thus solution: For each test case, compute intersection of character sets of A and B. If non-empty, print "Yes", else "No".
Time O(|A|+|B|) per test.
Double-check with possible edge cases: A = "a", B = "a" => common 'a' => "aa" palindrome? Actually "aa" is palindrome. Yes. But could there be any scenario where they share a character but the palindrome formed by two single characters "cc" is not considered valid because the concatenation is "cc" which is palindrome. So valid.
But what about the condition that s1 is substring of A, s2 substring of B. If we take s1 = "c", s2 = "c", both are substrings. So valid.
Thus it's trivial.
But hold on: Is there any hidden condition that s1 and s2 must be chosen such that s1 is a substring of A (contiguous) and s2 is a substring of B, but they can be taken from anywhere, not necessarily distinct occurrences. So yes.
But what if the strings are like A = "x", B = "xx"? They share 'x', so yes.
Seems too easy for a CodeChef problem? Possibly there is a twist: The concatenation s1+s2 must be palindrome, but we are allowed to choose any non-empty substrings, not necessarily of length 1. The trivial solution uses length 1 each. That is valid.
Thus answer is Yes if the two strings share any character.
But wait: Could there be a scenario where they share a character, but the problem expects "No"? Let's read the problem statement carefully: "Chef is eager to know whether it is possible to choose some non empty strings s1 and s2 where s1 is a substring of A, s2 is a substring of B such that s1 + s2 is a palindromic string." It does not impose any other constraints. So indeed.
But the sample test cases match this reasoning: Test1: "abc" and "abc" share a,b,c -> Yes. Test2: "a" and "b" share none -> No. Test3: "abba" and "baab" share many -> Yes.
Thus it's consistent.
However, before finalizing, let's consider a potential pitfall: The concatenation s1+s2 must be palindrome. If we choose s1 = "c" and s2 = "c", we get "cc". That's palindrome. But what if the palindrome definition includes reading forward and backward; "cc" reads same forward and backward. So yes.
But what if the strings contain uppercase? No, only lowercase.
Thus solution: For each test case, if set(A) ∩ set(B) ≠ ∅, output Yes else No.
But wait, is there any possibility that we cannot pick s1 = "c" from A and s2 = "c" from B because the character c might appear in both but we need substrings, and if the occurrence of c in A is at some position and in B is at some position, we can just take that single character. Substring of length 1 is allowed.
So yes.
Thus code is straightforward.
But hold on, is there any scenario where the palindrome "cc" is not considered valid because s1 and s2 must be non-empty but maybe they must be chosen such that s1 is a substring of A and s2 of B, but we must ensure that the concatenation is a string that is a palindrome; no further restrictions.
Thus answer is trivial.
However, why would the problem be presented? Perhaps the original problem had additional constraints like s1 must be a prefix of A and s2 suffix of B? But no.
Maybe I recall a CodeChef problem "CHEFPD" or something similar? I think there is one called "Palindromic Substrings" but not this.
Given the constraints up to 1000, a trivial O(N^2) is not needed. But they could have intended a more involved solution, but the necessary condition we found shows it's always sufficient.
Let's double-check the necessary condition more formally:
Let c = s1[0] (first character of s1). This character belongs to A. Let d = s2[|s2|-1] (last character of s2). This belongs to B. For palindrome, c must equal d. Thus there exists a character that appears in A and B. So necessary.
Sufficient: If there exists character c in A ∩ B, pick s1 as the substring consisting of that single character (any occurrence), similarly s2. Then s1+s2 = c c, which is palindrome.
Thus condition is both necessary and sufficient.
Therefore the answer is Yes iff A and B share at least one common character.
Edge Cases: Strings may be empty? But constraints say 1 ≤ |A|,|B|, so non-empty.
Thus solution is O(|A|+|B|) per test.
Let's produce Python code:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
A = next(it)
B = next(it)
if set(A) & set(B):
out.append("Yes")
else:
out.append("No")
sys.stdout.write("\n".join(out))
That's it.
But wait, is there any scenario where the common character condition holds but we cannot pick both substrings as single characters because we need s1 and s2 to be substrings but maybe they need to be chosen such that s1 is from A and s2 from B and they are distinct? No, they can be same character.
So fine.
But sometimes in programming problems, they might consider that the substring must be of length at least something? No.
Thus solution is correct.
Nevertheless, I'm slightly concerned because the problem might be a trick: Actually, what if the common character appears only once in each string but we need to use the same occurrence for both? But we are allowed to choose any substring from each string independently. So we can pick that character from each string; they don't have to be the same occurrence across strings. So it's fine.
Let's test with a custom case: A = "a", B = "ab". Common 'a'. Pick s1="a" from A, s2="a" from B? Does B have substring "a"? Yes, at index 0. So "aa" palindrome. Yes.
Another: A = "ab", B = "bc". Common 'b'. s1="b" from A (index1), s2="b" from B (index0) => "bb". Yes.
Thus works.
But wait, what about the possibility that s1 and s2 could be chosen such that concatenation is a palindrome of odd length, like "aba"? That would require s1="ab", s2="a". That's more complex, but our simple case always gives even length palindrome of length 2. So no need for complex.
Thus answer is indeed as above.
However, the example explanation for test1 says: "One possible way of choosing s1 & s2 is s1 = "ab", s2 = "a" such that s1 + s2 i.e "aba" is a palindrome." That's a more interesting example, but they also could have chosen s1="a", s2="a" to get "aa". So it's still Yes.
Thus our reasoning is solid.
But why would they provide examples that are not the simplest? Possibly to mislead or to show that longer palindromes exist. But the existence of a simple one makes it Yes.
Thus the problem reduces to set intersection.
But is there any twist about substrings that must be non-empty and contiguous? Single character is fine.
Thus I'm confident.
But wait: What about the case where both strings consist of only one character, but they are different? Then no common, answer No. That's fine.
One more thought: Could there be a scenario where the common character exists, but the palindrome formed by "cc" is not considered valid because concatenation of two substrings: s1 is a substring of A, s2 of B. If we take s1 = "c" from A, s2 = "c" from B, the concatenation is "cc". That's a palindrome. So valid.
Thus solution is set intersection.
Now let's confirm the constraints: T up to 10, length up to 1000, so even if we did O(n^2) it's fine but we don't need.
Thus final code.
But the problem might be originally from CodeChef (maybe "Chef and Palindromes") and I recall some discussion that it's indeed trivial. I think I've seen a similar problem: "Palindromic Substrings" or "Chef and Two Strings" where answer is simply checking common character. Yes.
Therefore, answer.
Now produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
A = next(it)
B = next(it)
# if there is any common character, answer is Yes
if set(A) & set(B):
out_lines.append("Yes")
else:
out_lines.append("No")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()