We need to understand the problem: Given two strings X and Y of length N, composed of 'W' and 'B'. We need to construct a string Z of length N (also with 'W'/'B') that maximizes the sum of Hamming distances: H(X,Z)+H(Y,Z). Among those that achieve the maximum, we need the lexicographically smallest string (where 'B' < 'W'? Lexicographically smallest: Typically 'B' and 'W', need to know order. Usually in such problems, alphabetical order: 'B' < 'W' because B < W in ASCII? Actually B (66) < W (87) in ASCII, so yes, B < W. So lexicographically smallest means as early as possible choose 'B' over 'W'.)
We have infinite supply of extra balls: meaning we can choose any characters for Z.
We need to maximize sum of Hamming distances. For each position i, we have X[i] and Y[i]. Let's denote them as bits: we could treat W=0, B=1 (or vice versa). But careful: We need to maximize sum of distances: For each position i, contribution to sum is: [Z[i] != X[i]] + [Z[i] != Y[i]].
Let a = X[i], b = Y[i]. There are four possibilities for (a,b): (W,W), (W,B), (B,W), (B,B). For each, we can choose Z[i] as either W or B, and the contribution is: if Z = same as a? Actually compute: d = (Z != a) + (Z != b). So for each case:
Case (W,W): then if Z=W: d=0+0=0; if Z=B: d=1+1=2.
Case (B,B): similarly: Z=B: d=0; Z=W: d=2.
Case (W,B) or (B,W) (i.e., different): then if Z=W: d= (W!=a)+(W!=b). Suppose a=W, b=B: then (W!=W)=0, (W!=B)=1 => d=1. If Z=B: (B!=W)=1, (B!=B)=0 => d=1. So d=1 regardless.
Thus, for positions where X[i] == Y[i], we can get either 0 or 2 contribution; where they differ, we get 1 regardless.
So to maximize total sum, for each position where X[i] == Y[i], we should choose Z[i] opposite to that common character (so that we get 2). For positions where X[i] != Y[i], the contribution is fixed at 1, regardless of what we choose. Thus the maximum sum is: number of positions where X[i]==Y[i] * 2 + number of positions where X[i]!=Y[i] * 1. But note that the total positions N = same + diff. So max sum = 2same + diff = same + (same+diff)?? Actually 2same + diff. Since same+diff = N, max sum = same + N. Equivalent: N + same. Because same = N - diff, then 2*(N-diff)+diff = 2N - 2diff + diff = 2N - diff = N + (N - diff) = N + same. So yes, max = N + same.
But note that for diff positions, the contribution is always 1, so we can choose either character. To achieve the maximum sum, we must for each same position choose opposite of that common character. For diff positions, we can choose either W or B, both yield the same contribution 1. So the maximum sum is achieved by any Z that satisfies: For all i where X[i]==Y[i], Z[i] != X[i] (i.e., opposite), and for i where X[i]!=Y[i], Z[i] can be either.
Now, among all such Z, we need lexicographically smallest.
So we need to construct Z of length N: For each position i:
- If X[i]==Y[i] then Z[i] must be the opposite character: if X[i]=='W' then Z[i]='B'; else Z[i]='W'.
- If X[i]!=Y[i] then Z[i] can be either 'W' or 'B'. To get lexicographically smallest string, we want to choose the smallest character possible at each position, but subject to that? Since we are free to choose, we should always choose 'B' (the smaller) if it leads to lexicographically smaller string? However, need to consider: lexicographic order: Compare from leftmost. At the first position where we have a choice, choosing 'B' yields a smaller string than choosing 'W', regardless of later positions. So indeed we should always choose 'B' at such positions to minimize lexicographically. However, is there any scenario where choosing 'B' might conflict with the constraint for same positions? No, because for same positions we have forced choice. For diff positions, any choice is allowed. So to make lexicographically smallest overall, for each diff position we simply choose 'B'.
But wait: Is 'B' always the smallest? Yes, because ASCII 'B'=66, 'W'=87. So indeed 'B' < 'W'. So lexicographically smallest will try to put 'B' wherever possible.
Thus the solution seems trivial: For each test case, read X and Y strings, for each index i:
- if X[i]==Y[i] then Z[i] = opposite of X[i] (i.e., if X[i]=='W' then 'B', else 'W')
- else (different) then Z[i] = 'B'.
But we must confirm with example: X=WBWB, Y=WBBB.
Let's compute:
Pos1: X=W, Y=W -> same -> opposite: B
Pos2: X=B, Y=B? Actually X[2]=? X="WBWB": indices 0-based: W,B,W,B. Y="WBBB": W,B,B,B. So pos1: both W -> same -> B (correct). Pos2: X=B, Y=B -> same -> opposite: W. So Z[2]=W. Pos3: X=W, Y=B -> different -> choose 'B'? (We said choose B) -> so Z[3]=B. Pos4: X=B, Y=B? Actually Y[3]=B, X[3]=B -> same? Wait X[3]=B, Y[3]=B? Y="WBBB": letters: index0 W, index1 B, index2 B, index3 B. So index3 (fourth) is B and X[3] is B, so same -> opposite: W. So Z[4]=W. So Z = B, W, B, W => "BWBW". That matches example output "BWBW". Good.
But let's test some edge cases: Suppose X and Y are all different? Then forced positions? Actually all positions are diff. Then we would set Z all 'B'. So Z = all B's. That's lexicographically smallest. Does that achieve maximum sum? Let's compute: For each diff position, contribution=1 regardless. So total = N. Could we get higher by doing something else? The maximum sum = N + same, but same=0 => N. So yes, it's maximum. And any Z would yield N? Actually if we choose any character at each diff position, contribution is always 1. So any Z yields exactly N, because for each diff position, regardless of choice, it's 1. So all Z are optimal. The lexicographically smallest among all strings of length N is all B's, correct.
What about all same? Then forced opposite each position => Z all opposite. For X all W, Y all W => Z all B. That's unique so it's the answer.
Thus solution appears trivial.
But wait: is there any nuance? Could there be any better arrangement if we consider something else? Let's confirm: For diff positions, contribution is always 1. So yes, any choice yields same contribution, so to maximize sum we just need to maximize contributions for same positions, which we achieve by choosing opposite. So the set of optimal Z is exactly those strings that satisfy: for each i where X[i]==Y[i], Z[i] != X[i]. No further constraints. So lexicographically smallest among them is: for each position i, choose the smallest possible character subject to the constraint. Since for diff positions there is no constraint, smallest is 'B'. For same positions, constraint forces that we cannot choose the same as X[i] (which is also Y[i]). But we might still be able to choose 'B' if the opposite is 'B'? Actually the opposite character: if X[i] is 'W', opposite is 'B', which is smallest anyway; if X[i] is 'B', opposite is 'W', which is larger, so we are forced to 'W' (which is larger than 'B'). So the lexicographically smallest Z is as described: For same positions: pick opposite, which will be 'B' if X[i]=='W', else 'W'. For diff positions: pick 'B'.
Thus the solution is O(N) per test case.
But wait: There is a possibility of multiple Z that achieve the maximum sum? We've accounted.
Now, confirm with a test: X="BB", Y="WW". Then same? Actually positions: i0: B vs W -> diff; i1: B vs W -> diff. So both diff. Then Z="BB"? That's lexicographically smallest? Yes "BB". But could there be "BW"? "BB" is lexicographically smaller. So correct.
Now consider X="BW", Y="WB". Both diff, Z="BB". The sum: H(X,BB) = (B vs B? X[0]=B, Z[0]=B => same ->0; X[1]=W vs B -> diff ->1) so 1. H(Y,BB) = (W vs B ->1, B vs B ->0) sum=1. Total=2. Could we get more? All choices yield 2. So fine.
Now check X="WW", Y="WB": pos0: both W? Y[0]=W, X[0]=W -> same => forced opposite: B. pos1: X[1]=W, Y[1]=B -> diff => choose B (smallest) => Z="BB". Sum: H(WW,BB)=2; H(WB,BB): W vs B=1, B vs B=0 => total=3. Max possible? same positions count=1, N=2 => max sum = 2+1? Actually N+same=2+1=3, yes. So optimum.
But is there any scenario where choosing 'B' on a diff position might affect the ability to maximize something else? No.
Thus the solution is extremely simple. However, note constraints: T up to 3 only, N not given explicitly, but strings length up to maybe large? Not given. But since simple O(N) is fine.
Thus we can implement.
Wait, is there any twist? The problem statement: "Akhil has many balls of white and black colors... he arranged the balls into two rows both consisting of N number of balls... he wants to create another row of N balls, Z in such a way that the sum of hamming distance between X and Z, and hamming distance between Y and Z is maximized." And then "As there can be multiple such arrangements of row Z, Akhil wants you to find the lexicographically smallest arrangement which will maximize the above value." So indeed our reasoning matches typical.
But we need to be careful about lexicographic ordering: usually 'B' < 'W', but is it specified? Not explicitly, but by standard string comparison in programming, it is alphabetical order. So it's safe.
Thus answer: For each test case, loop over characters of X and Y simultaneously, build Z as described.
Now, verify with some random tests: Write a brute-force for small N to see if our solution indeed yields max sum and lexicographically smallest among optimal.
For small N=3, all combos. But we'll trust.
Edge: Input may have trailing spaces? Use getline? Better to read strings normally.
Implementation details: T up to 3, string lengths can be large maybe up to 10^5? But not specified. Use Python (or C++). We'll do Python for simplicity.
Pseudo:
def solve():
import sys
data = sys.stdin.read().split()
t = int(data[0])
idx = 1
out = []
for _ in range(t):
X = data[idx]; Y = data[idx+1]; idx+=2
Z = []
for a,b in zip(X,Y):
if a == b:
# opposite
if a == 'W':
Z.append('B')
else:
Z.append('W')
else:
Z.append('B')
out.append(''.join(Z))
sys.stdout.write('\n'.join(out))
That's it.
Double-check: For diff positions, always 'B' is smallest, correct. However, consider if the opposite in same positions is 'W', which is larger, but we can't choose 'B' because that would be same as X[i]? If X[i]=='B', then opposite is 'W', but could we choose 'B'? That would give contribution 0? Let's check: if X[i]==Y[i]=='B', then choosing Z[i]='B' yields d=0, whereas choosing 'W' yields d=2. So 'B' is not optimal because it would lower sum. So forced to 'W'. So we can't choose 'B' even though it's smaller lexicographically. So we must stick to opposite. So algorithm stands.
Thus solution.
Now, there's an alternative approach: For diff positions, could we choose something else to possibly make later positions lexicographically smaller? No, because lexicographic order is determined by first difference. At diff positions, we can choose either; to minimize the string, at the earliest position where we have a choice, we want the smallest possible character. That would be 'B'. Since we can set it to 'B', it will be smaller than any string that has 'W' at that position (regardless of later). So the optimal strategy: For each position, choose the smallest character allowed (subject to maximizing sum). For same positions, allowed character is exactly the opposite, which might be 'B' or 'W'. So we pick that. For diff positions, allowed set = {'B','W'} both allowed; pick smallest = 'B'. So indeed.
Thus problem solved.
But wait: Could there be any scenario where picking 'B' at a diff position leads to not achieving the maximum sum? No, because contribution is 1 either way.
Thus answer trivial.
But is there any alternative interpretation? Possibly the sum they want to maximize is H(X,Z)+H(Y,Z) and they have infinite extra balls meaning they can also add balls? But Z is also N length, not longer. So infinite supply is irrelevant, just means we can pick any color.
Maybe they intended something else? But given example, it matches.
Thus solution is ready.
Now, let me think if there's any hidden trick: The problem might be from CodeChef? Possibly. I recall a problem called "HAMM" or something. Actually there is CodeChef problem "MAXHAM" maybe. But anyway.
Thus final answer.
I'll produce a solution explanation and code.
But the user wants "The assistant should provide the final answer." Possibly they want just the code. However, as per instruction: "You are ChatGPT, a large language model... Provide answers in a final answer message." They want solution.
I think output the code and explanation.
Given the problem is simple, maybe they want only output? The typical format: output string per test case.
We'll produce Python code.
But also consider T up to 3, but length may be up to 10^5, need to be efficient. Use sys.stdin.read.
Now, verify with more tests:
Test 1: Input:
1
WWW
BBB
X=WWW, Y=BBB. All positions: X=W, Y=B diff. So diff all positions -> Z should be all 'B' -> "BBB". Sum: H(WWW,BBB)=3, H(BBB,BBB)=0 => total=3. Max? same=0 -> N=3 -> max=3. Lexicographically smallest among all optimal? Any Z gives total=3? Check: For diff positions, any choice yields 1 each, so total always 3? Actually each diff position yields 1 regardless, so total=3 for any Z. So all Z are optimal. Lexicographically smallest string is "BBB". So correct.
But is there any Z that yields more than 3? Could we do something else? No.
Test 2: Input:
1
WB
WB
Both same? Actually X=W,B; Y=W,B -> same both positions. Then Z must be opposite: pos0: opposite of W -> B; pos1: opposite of B -> W -> "BW". Sum: H(WB,BW)=? W vs B diff=1, B vs W diff=1 => total=2; H(WB,BW)= same =2 => sum=4. N=2, same=2? Actually same positions count=2 (both same). Max = N+same =2+2=4. Good. Lexicographically smallest? Forced, only "BW". So output "BW".
Test 3: Input:
1
WB
BW
Positions: 0: W vs B diff, 1: B vs W diff. Both diff. So Z should be "BB". Sum: H(WB,BB)= (W vs B=1, B vs B=0)=1; H(BW,BB)= (B vs B=0, W vs B=1)=1 => total=2. Max: N=2, same=0 => max=2. Lexicographically smallest among all optimal? Could also be "BW"? Check "BW": H(WB,BW)= (W vs B=1, B vs W=1)=2; H(BW,BW)=0 => total=2 also. Actually careful: For diff positions, any Z yields sum=2? Let's compute with Z="BW": H(WB,BW): pos0: W vs B=1, pos1: B vs W=1 => 2; H(BW,BW): both same => 0 => total=2. So both "BB" and "BW" yield 2. Also "WB": H(WB,WB)=0; H(BW,WB)= (B vs W=1, W vs B=1)=2 => total=2. And "WW": H(WB,WW)= (W vs W=0, B vs W=1)=1; H(BW,WW)= (B vs W=1, W vs W=0)=1 => total=2. So all 4 strings give sum 2? Actually compute for each diff position: For each position i, contribution = [Z[i]!=X[i]] + [Z[i]!=Y[i]]; when X!=Y, that sum is 1 regardless? Let's verify: If X=W, Y=B. For Z=W: sum = (W!=W=0)+(W!=B=1)=1. Z=B: (B!=W=1)+(B!=B=0)=1. Yes, always 1. So each diff position contributes exactly 1, irrespective of Z. So total sum = number of diff positions. So indeed for all Z, sum is diff count. So any Z is optimal. Then lexicographically smallest is all B's, i.e., "BB". So correct.
Now, verify with a mixed case: X="WWB", Y="WBB". Compute: pos0: both W -> same -> B; pos1: W vs B diff -> choose B; pos2: B vs B same -> opposite: W. So Z = B,B,W => "BBW". Sum: H(WWB,BBW): pos0 W vs B diff=1, pos1 W vs B diff=1, pos2 B vs W diff=1 => 3; H(WBB,BBW): pos0 W vs B diff=1, pos1 B vs B same=0, pos2 B vs W diff=1 => total=2; sum=5. N=3, same positions count: pos0 same, pos2 same -> same=2? Actually pos0 same (W,W), pos2 same (B,B) => same=2; N+same=5, matches. Lexicographically smallest among optimal? Could we have chosen something else for diff pos1? We chose B. If we chose W, Z would be "BWW". Compare "BBW" vs "BWW": At index0 both B, index1: B vs W -> B < W, so "BBW" is lexicographically smaller. So indeed choose B is better.
Thus correct.
Now, any edge case: Strings might be of length 0? N maybe 0? Unlikely, but maybe. If N=0, empty string, output empty string. Our loop will produce empty.
Thus solution straightforward.
But wait: Is there any possibility that for diff positions, choosing 'B' might cause the string to not be lexicographically smallest because of the forced opposite later? Lexicographic order is determined by the first differing character. For any two candidate strings that are both optimal, compare them from leftmost. At the first position where they differ, the one with smaller character is lexicographically smaller. So if we construct Z by always choosing the smallest allowed character at each position, we get the lexicographically smallest overall. This is a greedy principle: to minimize lexicographically, you want to minimize the earliest character as much as possible, then the next, etc. Since the constraints are independent per position (except that later positions have no effect on earlier choices), we can greedily choose the smallest allowed character at each position. Allowed character set per position: if same => exactly one char (opposite). If diff => {'B','W'}. So smallest of allowed: same case: whatever the opposite is; diff case: 'B'. So our construction yields the lexicographically smallest overall.
Thus correct.
Now implement in Python.
We should handle input reading properly. Use sys.stdin.read().split() to get tokens. Strings may be long; but splitting by whitespace works.
We'll produce final answer.
But the instruction: "Provide answers in a final answer message." So I will output the code and brief explanation.
Let's write a solution.
Potential pitfalls: None.
Now, there may be tricky part: Lexicographically smallest definition: In some problems they consider 'W' < 'B'? Usually alphabetical: 'B' then 'W' (since B=66, W=87). Unless they define something else, but they didn't. So we assume standard.
Double-check example: Output "BWBW". If we had used 'W' as smaller, we would produce something else? For diff positions we would choose 'W'? That would give "WWWW"? Actually for example, diff positions were only at pos2? Wait example had one diff at pos2? Actually we computed diff at pos3? In example X=WBWB, Y=WBBB. Positions: 0 same->B, 1 same->W, 2 diff->? We chose B, 3 same->W -> "BWBW". If we had chosen 'W' for diff, we would get "WBWW"? Actually: pos2 diff: if we choose 'W', then Z = B, W, W, W = "BWWW"? Wait: pos0 B, pos1 W, pos2 W, pos3 W => "BWWW". That is lexicographically compared to "BWBW": at index0 both B; index1 both W; index2: 'W' vs 'B'? Actually "BWWW" index2 is W, "BWBW" index2 is B. Since B < W, "BWBW" is smaller. So "BWBW" is indeed lexicographically smaller than "BWWW". So our algorithm picks B for diff positions, which yields B at index2. So that is lexicographically smaller. So consistent with 'B' < 'W'.
Thus fine.
Now implement.
One more check: Could there be any scenario where opposite of same is 'W' but we would rather choose 'B' if allowed? Not allowed because it would reduce sum. So not allowed.
Thus final solution.
I'll produce the code.
But wait, maybe the problem expects a more complex solution? Could there be something like "maximizing sum of Hamming distances" but they have infinite supply of extra balls meaning they can also insert extra balls? No, Z has length N, same as X and Y. So infinite supply just means we can choose any color without limitation.
Thus final answer.
Now produce the final output. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
idx = 1
out_lines = []
for _ in range(t):
X = data[idx]
Y = data[idx + 1]
idx += 2
z_chars = []
for a, b in zip(X, Y):
if a == b:
# forced opposite
z_chars.append('B' if a == 'W' else 'W')
else:
# free choice: pick smallest 'B'
z_chars.append('B')
out_lines.append(''.join(z_chars))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()