We need to solve each test case: Given two distinct strings s and t of length n, we can swap s_i and t_j (any positions) at most 2n times. We need to make s and t equal. Determine if possible; if yes output sequence of swaps.
Observation: This operation swaps characters between s and t at arbitrary positions. After operations, we need s == t. Let's think: The goal is to make both strings equal. Essentially we have two multisets? Actually after operations, s_i = t_i for all i. The operation allows moving characters between s and t.
We can think of the positions of characters across both strings. Initially, there are 2n positions: n in s, n in t. After swaps, we want each pair of positions (s_i, t_i) to have same character. However, we can rearrange characters arbitrarily? The operation swaps s_i and t_j. That is like we can swap any character from s with any character from t. So we can rearrange characters between the two strings arbitrarily, but we cannot swap two positions both in s or both in t directly. However, we can simulate any permutation of the 2n characters? Let's see: The allowed operation is a swap between an element in set S and an element in set T. This is like we have two piles; we can exchange any two elements from different piles. With such swaps, we can achieve any configuration where the multiset of characters in S and T each remain the same? Actually swapping s_i and t_j swaps the characters. So the multiset of characters in s and t can change: a character from s moves to t, and one from t moves to s. The total multiset across both strings (2n characters) remains constant. Also the individual sets can change: after series of swaps, the set of characters in s can be any multiset that is a subset of the total, as long as |s| = n? But we are only swapping, so the size of s and t remain n each. The operation effectively allows us to permute the 2n characters arbitrarily? Consider we have two arrays of length n; we can perform swaps between them. This is analogous to having a bipartite graph with complete edges between S positions and T positions, and we can swap items across sides. If we think of all positions as being in a combined set, but the operation swaps two items in different sides. With enough swaps, we can reorder arbitrarily? For instance, if we want to move a character from s_i to s_j (within s), we could swap s_i with some t_k (moving character to t), then swap s_j with that t_k (moving character to s_j), then maybe swap back t_k with some other to restore. So essentially any permutation of the 2n characters is achievable using swaps across sides, because the swaps generate the full symmetric group? Actually the allowed swaps are only between S and T. The set of all transpositions (i, j) where i in S, j in T. The group generated is the whole symmetric group on 2n elements? Not exactly if we restrict to bipartite swaps; the graph is complete bipartite between two sets of size n. The transpositions that swap an element from left with an element from right generate the whole symmetric group if n>=2? Let's check: For n=1, we have only two elements, one swap across sides is allowed, that's the whole group. For n>=2, we can swap any two elements both in S? For example, to swap s_i and s_j, we can: swap s_i with t_1, then swap s_j with t_1, then swap s_i (now at t_1? need careful) Let's plan: Initially s_i=a, s_j=b, t_1=c. Goal: s_i=b, s_j=a. Steps: swap(s_i, t_1) => s_i=c, t_1=a. swap(s_j, t_1) => s_j=a, t_1=c. swap(s_i, t_1) => s_i=b? Wait s_i=c currently, t_1=c? Actually after second swap, t_1=c? Let's compute:
- Start: s_i=a, s_j=b, t_1=c.
- Swap s_i and t_1: s_i=c, t_1=a.
- Swap s_j and t_1: s_j=a, t_1=b.
- Now s_i=c, s_j=a, t_1=b. We want s_i=b, s_j=a? Actually we wanted s_i=b, s_j=a. We have s_j=a already. To get s_i=b, we can swap s_i with t_1: s_i becomes b, t_1 becomes c. Then final: s_i=b, s_j=a, t_1=c. So indeed we swapped s_i and s_j using three swaps with a t position. So any permutation can be achieved, but the number of swaps may be up to something like 3*(desired moves). But we are limited to 2n swaps, which is O(n). So we cannot arbitrarily permute all 2n characters if that requires many swaps. However, maybe the condition for feasibility is something about character counts? Since total characters across both strings, we need that for each character, its total count is even? Because eventually s = t, so each character appears the same number of times in both strings; that means for each character, count in s equals count in t. But total count across both is 2 * count_in_s (since they equal). So total count of each character must be even. However, is that sufficient given we can perform at most 2n swaps? Possibly not; we also need to be able to achieve equality within 2n swaps.
Let's examine constraints: n ≤ 50, at most 2n swaps. So we need a constructive method that uses O(n) swaps.
Observation: The operation swaps s_i and t_j. We can treat this as a problem of correcting mismatched positions. Let's define mismatches: positions i where s_i != t_i. The goal is to fix them.
One approach: Use a two-phase method: First, try to pair up mismatches. There is known Codeforces problem "Ujan and Problem 2 (hard version)"? Actually this is Codeforces problem 1244C? Not sure. I recall a problem "Make Them Equal" with swaps between s and t. There is a known solution: If for each character, its total count is even, then we can do it within 2n operations. And the construction: For each i from 1 to n, if s_i == t_i, skip. Else we need to fix. We'll find a position j>i such that s_i == s_j or s_i == t_j, etc. The typical solution for the problem where you can swap s_i and t_i? But here swap is between s_i and t_j, which is more flexible.
Wait, there is known problem: "Ujan and Problem (hard version)" from Codeforces Round #588? I'm not sure. Let's think.
Given we can swap any s_i with any t_j, we can think of it as having a pool of characters. Actually we can rearrange s arbitrarily? Because we can bring any character from t into s by swapping. And we can send any character from s to t. So effectively we can choose any multiset of n characters for s from the total 2n, as long as total counts match. So the problem reduces to: Is there a way to partition the multiset of 2n characters into two equal strings (s and t) such that s = t? That is simply: the multiset must be divisible into two identical multisets. That means the total count of each character must be even. Because we can assign exactly half of each character to s and the other half to t. So the necessary condition is: for each character, its total count across s and t is even.
Is it also sufficient? Provided we can achieve such a distribution with at most 2n swaps? Possibly yes if we can construct a sequence.
But we must also output the swaps. The operation allows any swapping across strings. So if the condition holds, we should be able to make them equal.
Let's test with examples:
Example 1: n=5, s="souse", t="houhe". Count letters? Let's compute: s: s o u s e -> s:2, o:1, u:1, e:1. t: h o u h e -> h:2, o:1, u:1, e:1. Totals: s:2, o:2, u:2, e:2, h:2? Actually h appears 2 in t, 0 in s -> total h=2 (even). s appears 2 in s, 0 in t -> total s=2 (even). o:1+1=2 even, u:1+1=2 even, e:1+1=2 even. So condition holds. They output Yes with 1 swap: 1 4. Let's see: swap s_1 (s) with t_4 (h)? t_4 is h? t="houhe": positions: 1:h,2:o,3:u,4:h,5:e. So swap s_1 (s) with t_4 (h) results in s becomes h o u s e? Actually s: s o u s e, after swap s_1 becomes h, t_4 becomes s. So s = "house"? That is h o u s e? Wait s_1 becomes h, s_2 o, s_3 u, s_4 s, s_5 e => "house". t becomes? t: h o u s e? Actually t_4 becomes s, so t becomes h o u s e? Check: t originally: h o u h e, after swapping t_4: becomes s, so t: h o u s e. So both become "house". So indeed they become equal. So condition holds.
Second example: cat, dog. Count totals: c:1, a:1, t:1, d:1, o:1, g:1 => all odd? Actually each appears once, so totals are 1 which is odd, so condition fails => No.
Third: aa, az. s: a,a; t: a,z. totals: a:3 (2+1) odd; z:1 odd -> condition fails => No.
Fourth: abc, bca. Count: a:1 (s) +1 (t)=2 even; b:1+1=2 even; c:1+1=2 even. So condition holds. They output Yes with 3 swaps: (1,2),(3,1),(2,3). Let's verify: n=3. s="abc", t="bca". Swaps:
- swap s1 (a) with t2 (c? t="bca", t2=c): s becomes c b c? Actually s: a b c, after swap s1 with t2: s1 becomes c, t2 becomes a => s="cbc", t="b a a"? Wait t becomes: originally t1=b, t2=c, t3=a. After swap: t2 becomes a, so t = "b a a"? Actually t1=b, t2=a, t3=a? That seems off: t3 remains a. So t = "baa"? That's not matching. Let's compute step by step.
Start:
s: a b c
t: b c a
Swap(1,2): swap s1 and t2. s1 becomes t2 = c; t2 becomes s1 = a.
Now:
s: c b c? Wait s2 remains b, s3 remains c, so s = c b c? Actually s = [c, b, c] -> "cbc".
t: b a a? t1=b, t2=a, t3=a -> "baa".
Second swap: (3,1): swap s3 and t1. s3 currently c, t1 currently b. So s3 becomes b, t1 becomes c.
Now:
s: c b b? s = [c, b, b] -> "cbb".
t: c a a? t = [c, a, a] -> "caa".
Third swap: (2,3): swap s2 and t3. s2 currently b, t3 currently a. So s2 becomes a, t3 becomes b.
Now:
s: c a b -> "cab".
t: c a b -> "cab". So equal. So works.
So condition seems necessary and sufficient.
But we need to confirm: Are there any cases where totals are even but still impossible? Let's think. Since we can arbitrarily reassign characters between s and t via swaps, we should be able to achieve any distribution of characters across s and t that respects the total multiset. As long as the total counts for each character are even, we can aim to make s = t. That is, we want to end with s containing exactly half of each character. Since the total count of each character is even, half is integer. The total multiset is fixed; we need to move characters to achieve that exact distribution.
The question reduces to: Given the initial configuration, can we transform it into the target configuration (where s = t) using swaps between s and t, with at most 2n swaps? Since the target is specific: For each position i, we need s_i = t_i, but the actual characters on each position can be arbitrary as long as the two strings are identical. That is, any pair of strings that are equal is acceptable. So we can choose the final equal strings arbitrarily, as long as they use exactly half of each character (if we require s and t to be exactly the same string, then each character appears twice as many as in the final string? Actually final s and t must be identical. So if we let final string be some string p of length n, then s = p, t = p. So the multiset of characters in p is exactly half of total for each character. So p is any string where each character c appears exactly total_c / 2 times. So there may be many possible p. So we can choose any p that fits the total counts.
Thus, if total counts even, we can pick a target p. The challenge is to achieve it with ≤ 2n swaps.
We need to devise an algorithm that uses at most 2n swaps. That is quite tight. The provided examples used 1 and 3 swaps respectively; 3 ≤ 2n=6. So it's possible.
We need to construct a sequence. Let's think about the operation: swapping s_i and t_j. This is like we have two strings and we can exchange any two characters across them. This is similar to having two arrays and we can rearrange by moving mismatches.
One approach: For each position i from 1 to n, we want to set s_i and t_i to be the same character. Suppose we have already fixed positions 1..i-1 such that s_j = t_j for j < i. Now consider position i. If s_i == t_i, we move on. Else we need to make them equal.
We have s_i (call it a) and t_i (call it b), with a != b. We need to change either s_i or t_i to match the other. Since we can swap s_i with some t_j (j maybe i or other) and t_i with some s_j.
Idea: Find a swap that will set both to the same character, maybe using a character elsewhere.
We need to do it efficiently. Since we have at most 2n swaps total, we can afford about 1 or 2 swaps per mismatch? There can be up to 2n mismatches? Actually maximum mismatches when s_i != t_i? But note s and t are strings of length n, so at most n positions where they differ? Actually they could differ in all n positions, but then there are n mismatches. But we are allowed up to 2n swaps, which is about 2 per mismatch. That's plausible.
But the operation swaps s_i with t_j, not necessarily t_i. So we can fix mismatches by swapping with some other position.
Consider position i where s_i != t_i. We want to make them equal. There are two strategies:
-
If there exists some j (maybe j > i) such that s_j == s_i (i.e., s has the same character as s_i), then we could swap s_j with t_i. That would bring that character to t_i, making t_i = s_i, but then s_j becomes b (original t_i). Now s_j and t_j may become mismatched, but we can handle later. However, this uses 1 swap and fixes position i (since now s_i == t_i = a). But t_i becomes a, s_i unchanged a. So position i becomes matched. However, we have disturbed position j: s_j changed from a to b, and t_j unchanged. If originally s_j == t_j? Maybe not. We'll handle later.
-
Alternatively, if there exists some j such that t_j == s_i, we could swap s_i with t_j. That would set s_i to something else? Actually if we swap s_i (a) with t_j (a), then nothing changes? Actually if t_j == a, then swapping results: s_i becomes a (same), t_j becomes a (same) – no change. So not helpful. So we need different.
Better: If we want to make s_i and t_i equal, we could try to bring a copy of b to s_i or a copy of a to t_i.
If there is some j (j != i) such that s_j == b, we could swap s_j with t_i. Then t_i becomes s_j = b, and s_j becomes original t_i = a. After that, s_i (a) and t_i (b) are still different? Actually we wanted them equal; we might aim to set both to b? Or both to a? After swap, t_i becomes b, s_i is a. So still not equal. So that didn't help.
If we swap s_i with t_j where t_j == b, then s_i becomes b, t_j becomes a. Then s_i = b, t_i = b? t_i originally b? Actually if t_i == b, then originally s_i != b. After swap, s_i becomes b, so s_i = t_i = b, matched. But t_j becomes a, which may cause mismatch.
So to fix position i, we can:
- If there exists j (j != i) such that t_j == s_i, then swapping s_i with t_j does nothing. Not useful.
- If exists j such that t_j == b (the character of t_i), then swap s_i with t_j to set s_i to b, matching t_i (if t_i == b). But t_i currently is b? Wait b = t_i. So indeed if we swap s_i with a t_j that also equals b, then s_i becomes b, matching t_i (which is b), and t_j becomes a (original s_i). So that fixes position i. But note we require t_j == b and j != i? Could be j = i? If we swap s_i with t_i, then they exchange: s_i becomes b, t_i becomes a. Then they are swapped: s_i = b, t_i = a. That does not make them equal unless a=b. So not helpful.
Thus, fixing by setting s_i to b requires a t_j == b for some j != i. Similarly, we could set t_i to a by swapping t_i with s_j where s_j == a (j != i). That would set t_i = a, matching s_i = a. So either method works.
But we need to ensure existence of such j. Since total counts of each character are even, perhaps we can guarantee such j exists among the remaining positions if we haven't fixed everything? However, it's possible that at a given moment, the only occurrence of a or b might be at position i itself. But we haven't fixed other positions yet, and the total counts are even, so there must be another occurrence of a somewhere among the other positions (either in s or t) because total count of a is even; currently we have one a at s_i, and maybe one at t_i? t_i is b, not a. So total a's: s_i contributes 1. Since total count of a is even, there must be another a somewhere in either s or t (excluding position i). That other a could be in s_j (j != i) or in t_k. Similarly for b. So there is at least one other occurrence of a and one other occurrence of b. But that other occurrence might be in s or t. If the other a is in s_j, then we could swap t_i with s_j to set t_i = a, fixing i. If the other a is in t_j, then swapping s_i with t_j? That would set s_i to a? Actually if t_j == a, then swapping s_i (a) with t_j (a) does nothing; not helpful. But we could instead aim to set both to b: need another b somewhere. If the other b is in t_j, then swapping s_i with t_j sets s_i to b, fixing i. If the other b is in s_j, then swapping t_i with s_j sets t_i to b? Actually t_i is b originally, so swapping t_i with s_j where s_j == b would do nothing? Wait: if s_j == b, swapping t_i (b) with s_j (b) does nothing. Not helpful.
So the method works if there exists another occurrence of the needed character in the opposite string: To set t_i = a, we need an a in s (other than s_i). To set s_i = b, we need a b in t (other than t_i). Since total counts are even, we can guarantee at least one of these? Not always both: It could be that the only other a is in t, and the only other b is in s. Then neither direct fix works? But maybe we can do a two-step fix.
Consider scenario: n=2, s="ab", t="ba". totals: a:2, b:2 (even). At i=1: s1='a', t1='b'. Other a: s2? s2='b', not a. t2='a' (yes, t2='a'). So other a is in t2. Other b: s2='b', t2? t2='a', not b. So other b is in s2. So to fix i=1, we cannot directly set t1 = a because need a in s (other than s1) but the other a is in t2, not s. And cannot set s1 = b because need b in t (other than t1) but the other b is in s2, not t. So direct fix fails. However, we can perform a swap between s1 and t2 (s1='a', t2='a')? That does nothing. Or swap t1 with s2: t1='b', s2='b' does nothing. So need another approach.
But we know it's possible: For "ab" and "ba", we can swap s1 with t1? That yields s="bb", t="aa". Not equal. Then swap s1 with t2? Let's try: after first swap s1,t1: s="bb", t="aa". Then swap s1 with t2: s1='b', t2='a'? Actually after first, s="bb", t="aa". Swap s1 with t2: s1 becomes a, t2 becomes b => s="ab", t="ab"? Wait s becomes a b? s1='a', s2='b' => "ab". t becomes a b? t1='a', t2='b' => "ab". So two swaps. That works. So we need a method that works in general.
We can formalize: We need to turn the two strings into the same string. Since total counts even, we can always do it. Known solution from Codeforces problem 1249C2? Actually I recall a problem: "Make Them Equal" with operations swapping s_i and t_i? No. There is a problem "Ujan and Problem" where you can swap any s_i with any t_j, and you need to make them equal. The solution is: If for each character the count is even, answer is Yes, and you can do it in at most 2n moves. The construction: For each i from 1 to n, if s_i == t_i, continue. Else, we try to find j>i such that s_i == s_j, then swap s_j with t_i, and output that. If not, then find j>i such that s_i == t_j, then swap s_i with t_j, then swap s_j with t_i? Actually need to check.
Let's search memory: There's Codeforces Round #1105 problem D? Not sure.
I think I recall editorial: For each i, if s_i == t_i, skip. Else, look for a position j>i such that s_i == s_j. If found, swap s_j with t_i. This makes s_i == t_i (both equal to original s_i). Then continue. If not found, then we must have s_i == t_j for some j>i (since total count even ensures existence). Then we do two swaps: first swap s_i with t_j (which makes s_i become t_j which equals s_i? Actually if s_i == t_j, swapping does nothing, so maybe they do something else). Wait perhaps they treat case when s_i == t_j? That would be s_i equals some t_j, but then swapping s_i with t_j does nothing. So maybe they do: If there is no j>i with s_i == s_j, then there must be some j>i with t_i == t_j? Not sure.
Let's derive proper algorithm.
We have to achieve s_i == t_i for all i. Let's treat mismatches. Suppose we iterate i from 1 to n. At each step, we ensure that after processing i, s_i == t_i, and the suffixes (i+1..n) remain unchanged? Or we might modify later positions, but that's okay.
We can aim to fix each mismatch with at most 2 swaps. That would yield at most 2*(number of mismatches) ≤ 2n swaps. Since total mismatches ≤ n, we get ≤ 2n.
So we need a procedure to fix position i with at most 2 swaps, assuming that for all k < i, we have s_k == t_k, and maybe we haven't touched positions > i yet.
Let's denote current s_i = a, t_i = b, with a != b.
We want to end with both equal. We can use swaps that involve positions i and some j>i (or maybe j=i? not helpful). We'll consider j > i to avoid messing already fixed positions.
We have three types of characters elsewhere (positions > i) in s and t.
We'll consider two cases:
Case 1: There exists some j > i such that s_j == a. Then we can swap s_j with t_i. After swap: s_j becomes b, t_i becomes a. Now t_i equals a, and s_i is a, so s_i == t_i = a. Fixed. This swap affects s_j and t_i. t_i is now fixed. s_j changed from a to b, and t_j unchanged. But j > i, so we haven't fixed j yet; we'll handle later. So this uses 1 swap.
Case 2: There exists some j > i such that t_j == b. Then we can swap s_i with t_j. After swap: s_i becomes b, t_j becomes a. Then s_i == t_i? t_i is b, so s_i becomes b, so they match. This uses 1 swap.
If neither case holds, then there is no j>i with s_j == a, and no j>i with t_j == b. But because total counts of a and b are even, and we have already fixed positions < i (which are balanced), the remaining positions (including i) must contain the remaining counts. Let's analyze: For character a, total count even. At positions < i, they are balanced (s_k = t_k). So in those fixed positions, the number of a's in s equals number of a's in t? Not necessarily: In fixed positions, s_k = t_k, so each such position contributes either both characters same, so if that character is a, then both have a. So in the fixed prefix, the count of a in s equals count of a in t. So the difference between s and t in the suffix (i..n) must be zero for each character? Actually overall, total a in s equals total a in t? Not initially; initially total counts of each character across s and t are even, but not necessarily equal between s and t. However, after we achieve final equality, they must be equal. But currently, we haven't achieved equality. But we are in the process of constructing. The condition we maintain might be that after fixing prefix, the multiset of characters in the remaining positions (suffix) can be paired such that we can still achieve equality. I think it's okay.
Given total even, we can argue that if there is no j>i with s_j == a and no j>i with t_j == b, then there must be a j>i such that s_j == b and t_j == a? Or something like that. Because if the only occurrences of a in suffix are at s_i? But we have also t_i = b, not a. But there must be another a somewhere because total even. Since no s_j == a for j>i, the other a(s) must be in t positions > i. Similarly, the other b(s) must be in s positions > i. So there exists some j>i with t_j == a, and some k>i with s_k == b. Possibly j and k could be the same? If there exists a position j>i where s_j == b and t_j == a, that would be perfect: we can perform two swaps to fix i. Actually the classic solution I recall: If there is no direct single-swap fix, then we can first swap s_i with t_i? That would exchange a and b, giving s_i = b, t_i = a. Then we are in a situation where we can apply one of the above cases? But swapping s_i with t_i doesn't fix i; it just swaps them. But maybe after that, we can find a j>i with s_j == a (or t_j == b). Since originally there was no s_j == a, but after swapping, s_i becomes b, so s_j == a? Still no. Hmm.
Alternate method: If we cannot find s_j == a or t_j == b, then we can take any j>i (there must exist because n>=2 and we have at least one more position) and perform two swaps: first swap s_i with t_j, then swap s_j with t_i. Let's simulate: Starting s_i=a, t_i=b. Choose some j>i. Let s_j = x, t_j = y. After first swap (s_i, t_j): s_i becomes y, t_j becomes a. After second swap (s_j, t_i): s_j becomes b, t_i becomes x. Final: s_i=y, t_i=x, s_j=b, t_j=a. We want s_i = t_i =? Not necessarily. Not solved.
But if we specifically choose j where s_j = b and t_j = a? Then after first swap (s_i, t_j): s_i becomes a? Actually t_j = a, so s_i becomes a, t_j becomes a? Wait: s_i=a, t_j=a? That does nothing. Not good.
Better to choose j such that s_j == b and t_j == a (i.e., a cross match). Then we can do: swap s_i with t_j? s_i=a, t_j=a? No, t_j=a so swap does nothing. So that case not helpful.
Alternative two-swap sequence: Suppose we perform swap(s_i, t_i) first, resulting in s_i=b, t_i=a. Now we need to fix i, maybe we can find j>i with s_j == a (to set t_i = a? Already t_i = a, so we want s_i to become a? Actually after swap, s_i=b, t_i=a. To make them equal, we could set s_i to a (by swapping with t_j where t_j=a) or set t_i to b (by swapping with s_j where s_j=b). Since earlier we assumed no t_j == b (for j>i) and no s_j == a? Let's check: Before any swaps, we had condition: no j>i with s_j == a (original a) and no j>i with t_j == b (original b). After swapping s_i and t_i, s_i becomes b, t_i becomes a. Now we look for a j>i with t_j == b? Since originally there was no t_j == b, but after swap we haven't changed t_j for j>i. So still no t_j == b. And look for s_j == a? originally there was no s_j == a, so still none. So that didn't help.
Thus, we need a more clever approach.
Maybe we can use an invariant: After processing prefix, we maintain that for all positions k < i, s_k = t_k, and for positions >= i, the multiset of characters in s[i..n] and t[i..n] are such that each character appears an even number? Actually since total counts are even and prefix is balanced, the suffix must also have even counts? Let's check: Overall, for each character c, count_s(c) + count_t(c) is even. After fixing prefix where s_k = t_k, the number of c in the prefix is 2 * p_c (since each matched pair contributes 2). So the remaining characters in suffix (both strings) have total count even as well. However, the counts in s suffix and t suffix individually may not be equal. But for each character, the parity of the difference between s suffix and t suffix? The total in suffix for both strings is even; the difference (s_count - t_count) might be some number d. Since total even, d is even? Actually from overall: sum_s(c) + sum_t(c) = even. Also in prefix: s_prefix(c) = t_prefix(c) because they are equal. So s_prefix(c) = t_prefix(c). Let s_suf(c) = s_count(c) - s_prefix(c), similarly t_suf(c). Then s_suf(c) + t_suf(c) is even. But s_suf(c) and t_suf(c) might not be equal. But we need eventually to make them equal. The operations will adjust.
During processing, we want to gradually reduce mismatches.
Maybe a better approach: Instead of fixing positions one by one from left to right, we can think in terms of pairing mismatches directly.
Define mismatched pairs: For each i where s_i != t_i, we have a mismatch. We can try to resolve mismatches by swapping characters between mismatches.
Observation: If we have two mismatched positions i and j (i < j) such that s_i = s_j, then we can fix i with one swap: swap s_j and t_i. That gives t_i = s_i, and s_j becomes t_i (which is b originally). This might create a new mismatch at j or fix j if t_j happens to equal something? But after swap, s_j becomes b, t_j remains whatever. So now at j, we have s_j = b, t_j = ? originally t_j. This might become a match if originally t_j = b, but not necessarily.
Better: If we have two mismatches i and j with s_i = s_j, we could do: swap(s_j, t_i). This makes s_i == t_i (both a), and at position j, s_j becomes b. Now we might have a new mismatch at j, but perhaps we can then fix j using another method. But the total number of mismatches might reduce? After swap, at i, we fixed it. At j, we changed s_j from a to b. So if originally t_j was a, then now s_j = b, t_j = a, which is a mismatch; but if originally t_j = b, then now s_j = b, t_j = b => fixed. So we could reduce mismatches by 1 or 2.
Similarly, if we have two mismatches i and j with t_i = t_j, we can swap(s_i, t_j) to fix i.
If we have a mismatch i, and we cannot find such a same-character in the same string among mismatches, then we must have a cross-match: s_i = t_j and s_j = t_i for some j. Then we can perform two swaps: first swap(s_i, t_i) to exchange, then swap(s_i, t_j) or something. Actually standard solution for a similar problem (where you swap s_i and t_i) but here we can swap across. Let's recall known approach from Codeforces problem 1244B? Not sure.
Wait, maybe I recall a solution: For each character, if total count odd -> No. Else, we can do the following: For each i from 1 to n:
- If s_i == t_i, continue.
- Else, find a position j from i+1 to n such that s_i == s_j. If found, swap(s_j, t_i) and record.
- Else, find a position j from i+1 to n such that s_i == t_j. If found, then do two swaps: swap(s_i, t_j) and then swap(s_j, t_i). Actually check: if s_i == t_j, then swapping s_i and t_j does nothing because they are equal. So maybe they do: swap(s_i, t_i) then swap(s_i, t_j)? Let's test with example where s="ab", t="ba". i=1, s1='a', t1='b'. There is no j>1 with s1 == s_j because s2='b'. There is no j>1 with t1 == t_j? t1='b', t2='a' so no. But there is j with s1 == t_j? t2='a', so s1 == t2. According to the rule: else find j such that s_i == t_j. Then we can do: swap(s_i, t_i) (so now s="bb", t="aa"), then swap(s_i, t_j)? After first swap, s_i = 'b', t_j = 'a'? Actually j=2, t2='a' (unchanged). Swap(s_i, t_j) => swap(s1, t2): s1 becomes a, t2 becomes b. Then we have s="ab", t="ab". That's two swaps. So the sequence is: swap(s_i, t_i) then swap(s_i, t_j). But does that always work? Let's see.
General case: i, with s_i = a, t_i = b, a != b. Suppose we find j > i such that s_i == t_j (i.e., a == t_j). Then we can perform:
- Swap(s_i, t_i) -> now s_i = b, t_i = a.
- Swap(s_i, t_j) -> now s_i becomes t_j (which is a) and t_j becomes b. After step 1, t_j is still a (since we didn't change it). So swapping s_i (now b) with t_j (a) gives s_i = a, t_j = b. After these two swaps:
- Position i: s_i = a, t_i = a? Wait t_i after step1 became a, and step2 did not involve t_i, so t_i = a. So s_i = a, t_i = a -> fixed.
- Position j: originally s_j = ? and t_j = a. After step2, t_j becomes b. s_j unchanged. So now at j, we have s_j (original) and t_j = b. Did we improve? Not necessarily fixed, but we have reduced the mismatch at i. This operation uses 2 swaps and does not break the prefix? However, it might affect already fixed positions if j < i? But we only consider j > i. So it's okay. Also, we changed t_j from a to b. That might create a mismatch at j if it wasn't already mismatched, but j > i, we haven't processed j yet. So we'll handle later.
Now, what about the case where we cannot find s_i == s_j or s_i == t_j? Could there be such a case? Since total counts even, there must be at least one other occurrence of a somewhere. If there is no s_j == a for j>i, and no t_j == a for j>i, then the only other a must be at some position k <= i? But k < i are already fixed and have s_k = t_k, so if s_k = t_k = a, then that position contributes an a to both strings. But it's fixed; we could use it? But we might not want to disturb fixed prefix. However, maybe we can still find j > i because there must be an a in the suffix. Let's reason: The total number of a's in s and t is even. Consider positions from i to n. At position i, s has an a, t does not (since t_i = b). For the remaining suffix (i+1..n), if there is no a in s and no a in t, then total a count would be 1 (from s_i) which is odd, contradicting evenness (since fixed prefix have equal counts, so total even implies that the suffix must have odd count? Actually need careful: Let total count of a be even. In the prefix 1..i-1, s and t are equal. So number of a in prefix: let p = number of positions k < i with s_k = t_k = a. Then those contribute 2p a's. Also there could be positions k < i where s_k = t_k != a, they contribute 0. So prefix contributes even count. The total count is even, so the number of a in positions i..n must also be even. At position i, we have one a in s, and 0 in t (since t_i = b). So the total in suffix so far is 1. To make even, there must be at least one more a in the suffix (i+1..n). That a could be in s or t. So there exists some j>i such that either s_j = a or t_j = a. So we are guaranteed to have either s_j == a or t_j == a for some j>i. Similarly for b? Actually we might need to ensure existence of b? Not needed for this approach. So we have either case 1: s_j == a (exists) -> we can do 1 swap (swap(s_j, t_i)). Or case 2: no s_j == a but there is t_j == a -> then we can do the two-swap sequence: swap(s_i, t_i) then swap(s_i, t_j). But careful: In case 2, we have t_j == a, and we also need to consider that there might be s_j == something else? The two-swap sequence uses j but doesn't require anything about s_j. So it's fine.
But what about the scenario where we have both s_j == a and t_j == a? That's covered by case 1 (since s_j == a exists). So the algorithm is:
For i from 1 to n:
if s_i == t_i: continue.
else:
a = s_i, b = t_i.
Look for index j in [i+1, n] such that s_j == a.
If found:
swap(s_j, t_i) // one swap
record (j, i)
Else:
// then there must exist j in [i+1, n] with t_j == a (since total a even)
// pick such j
swap(s_i, t_i) // first swap
record (i, i)
swap(s_i, t_j) // second swap
record (i, j)
After this, s_i becomes a and t_i becomes a (fixed). We'll need to verify that after these swaps, s_i and t_i indeed become equal. Let's test both cases.
Case 1: swap(s_j, t_i). Before: s_i=a, t_i=b; s_j=a, t_j=? After swap: s_j becomes b, t_i becomes a. Now s_i = a, t_i = a => fixed. s_j becomes b, t_j unchanged. So position i fixed.
Case 2: first swap(s_i, t_i): s_i becomes b, t_i becomes a. Then swap(s_i, t_j): after first swap, s_i = b, t_j = a (since t_j == a). Swapping these: s_i becomes a, t_j becomes b. Now s_i = a, t_i = a (t_i unchanged after second swap) => fixed. So i fixed.
After processing i, we move to i+1. But note that we may have modified s_j or t_j for j>i. However, we don't care as long as we can later fix them. Also, note that after case 2, t_j becomes b. Could that affect the possibility to fix later? Possibly, but we'll handle later.
Now, we must ensure that after processing all i, the strings become equal. We need to verify that this algorithm works and never gets stuck, i.e., when we are at i and s_i != t_i, either a s_j == a exists for some j>i, or if not, then a t_j == a exists for some j>i. But is it possible that both are absent? As argued, total a count is even, and prefix has balanced a's? But we need to be careful: The prefix positions (<i) have been fixed, meaning s_k = t_k for k < i. However, after previous operations, the characters in the prefix may have changed? But we don't touch them again. They remain equal. However, the counts of characters in the prefix might not be balanced for each character? Actually if they are equal, then for each character c, the number of c in s_prefix equals number of c in t_prefix. So prefix contributes equal numbers to s and t. Therefore, the difference between counts of a in s and t in the suffix (i..n) must be such that overall counts can become equal after final steps. Initially before any operations, total counts of each character are even, but not necessarily equal between s and t. However, as we perform swaps, we are effectively moving characters between s and t, so the totals per string change. But we need to guarantee that at each step, the required j exists.
Let's analyze invariants. Suppose we maintain that after processing positions 1..i-1, we have s_k = t_k for all k < i. Also, the total number of each character in the remaining part (i..n) across both strings is even? Actually not necessarily. But we can consider the total count of each character in the entire strings. Since we are only swapping characters between s and t, the multiset of each character across both strings remains constant (even). Also, after we fix a position i, we ensure s_i = t_i, so that position becomes balanced. So after processing i, the prefix up to i is balanced. So for each character, the count in the prefix is even (since equal numbers in s and t). Therefore, the remaining suffix (i+1..n) also has total count even? Because total even minus even = even. But that's total across both strings, not necessarily balanced individually.
Now, at step i, we have s_i and t_i possibly different. Let a = s_i, b = t_i, a != b. Consider the suffix from i to n (including i). At position i, s_i contributes a, t_i contributes b. In the suffix (i..n), the total count of a is something. Since overall total a is even, and prefix (1..i-1) has even count of a (balanced), the total a in suffix (i..n) must be even as well. At position i we have one a in s and 0 a in t (since t_i = b). So number of a in positions i+1..n (both strings) must be odd (to make total even? Let's compute: total a in suffix = (a at s_i) + (a in s_{i+1..n}) + (a in t_{i+1..n}). Since total a in suffix is even, and we have one a at s_i, then the sum of a in s_{i+1..n} + t_{i+1..n} must be odd. That means there is at least one a in the rest (i+1..n). So there exists j>i such that either s_j = a or t_j = a. That's exactly what we need. So indeed, at each step, there is always some j>i with either s_j = a or t_j = a. So our algorithm's case distinction is exhaustive: either there is s_j = a (case 1) or if not, then there must be t_j = a (case 2). So we can always proceed.
But wait: Could it be that the only a in the rest is at a position that we have already modified earlier? But we only look for j>i, regardless of whether it's been modified, it's fine. Because after previous steps, some positions j>i may have been changed, but they still contain characters. As we process i from left to right, we may have already swapped some later positions when fixing earlier mismatches. However, that's okay; we are allowed to use them. The existence of an a in the suffix after processing up to i-1 is guaranteed by the invariant that total a in suffix is even? Let's check the invariant holds after each step.
We need to ensure that after we fix position i, the invariant that the total count of each character in the remaining suffix (i+1..n) is even? Actually after we fix i, we have s_i = t_i = a (since we set both to a). But wait, after fixing, s_i and t_i become a. But originally s_i = a, t_i = b. After our operations, we set them to a. So we are effectively removing a from the system? Let's track counts: In case 1: we used a s_j (which was a) and moved it to t_i, making t_i = a. We also changed s_j from a to b. So the net effect on character counts: At position i, we added a to t_i (changed from b to a), and removed b from t_i (b moved to s_j). At position j, we removed a from s_j and added b. So overall, the multiset of characters across both strings remains same. After fixing i, s_i = a, t_i = a, so position i now contributes two a's. In the suffix (i+1..n), the counts of a and b change. Specifically, a decreased by 1 (since we moved an a from s_j to t_i? Actually careful: Initially, a's: s_i (a), s_j (a) – two a's. After operation: s_i (a), t_i (a) – two a's. So total a count unchanged. b's: originally t_i (b), maybe elsewhere. After: s_j (b). So b count unchanged. So total counts remain even.
Now, what about the invariant for future steps? After processing i, we have prefix up to i balanced. For character a, the number of a in prefix (1..i) is even because s_i = t_i = a gives 2 a's, plus any previous balanced pairs. So prefix balanced. For any character, the total in suffix (i+1..n) remains even? Let's see: The total in suffix is total minus prefix. Since total is even, prefix is even, suffix is even. So invariant holds for all characters. Good.
Now for case 2: We perform two swaps. Let's check counts: Initially: s_i = a, t_i = b, t_j = a, and s_j = x (some character). After first swap (s_i, t_i): s_i=b, t_i=a. After second swap (s_i, t_j): s_i becomes a (since t_j=a), t_j becomes b. So final: s_i=a, t_i=a, s_j=x (unchanged), t_j=b. So we have moved a from t_j to s_i, and b from s_i to t_j. So overall, a's: originally at s_i and t_j; after: at t_i and s_i? Actually s_i=a, t_i=a, so two a's; t_j lost a, gained b. So total a unchanged. b's: originally at t_i; after: at t_j. So total b unchanged. So counts remain same. And prefix up to i becomes balanced (s_i=t_i=a). So invariant holds.
Thus the algorithm is sound.
Now we need to ensure that we do at most 2n swaps. Each mismatch i triggers either 1 or 2 swaps. There can be at most n mismatches? Actually it's possible that after some swaps, we create new mismatches? But our algorithm only modifies positions i and j (with j>i). For case 1, we modify t_i and s_j. t_i becomes fixed (since after swap, t_i = a, matching s_i). s_j changes from a to b. This may create a mismatch at j if s_j != t_j originally, or it may fix it if t_j == b. However, we do not count that as fixing i; we will later process j when i reaches j. Since j > i, we haven't processed it yet. So when we eventually get to j, it may be a mismatch or not. The number of mismatches overall may not necessarily decrease by 1 each time; it could stay the same or even increase? But we are processing each index in order, and we guarantee that when we finish processing i, s_i = t_i. So we never need to revisit i. The total number of operations is at most 2 per index i. Since there are n indices, total swaps ≤ 2n. But note: we only perform swaps when s_i != t_i. If s_i == t_i, we skip. So the number of swaps is at most 2 * (number of mismatches) ≤ 2n.
Thus the algorithm yields at most 2n swaps.
Now we need to output the swaps. Implementation: For each test case, check if each character total count is even. If not, print "No". If yes, simulate the algorithm, performing swaps on the actual strings (or just record swaps). We'll need to update the strings as we go because future decisions depend on current state. So we maintain mutable strings s and t (list of chars). Iterate i from 0 to n-1 (0-index). At each i, if s[i] == t[i], continue. Else:
- a = s[i]
- b = t[i]
- Find index j > i such that s[j] == a. Search from i+1 to n-1.
- If found: swap s[j] and t[i]. Record swap (j+1, i+1) because positions are 1-indexed.
- Else (must exist t[j] == a). Find j > i such that t[j] == a. Then:
- Perform swap(s[i], t[i]) (record (i+1, i+1))
- Then swap(s[i], t[j]) (record (i+1, j+1))
But wait: After swapping s[i] and t[i], s[i] becomes b, t[i] becomes a. Then we swap s[i] (now b) with t[j] (a). That yields s[i]=a, t[j]=b. Works. However, after first swap, t[i] becomes a. After second swap, s[i] becomes a, so final s[i]=a, t[i]=a. Good.
But we must ensure that after the first swap (s[i], t[i]), we haven't messed up the invariant for finding j? We already found j based on original state? The existence of t[j] == a is guaranteed in the original configuration before any swaps? But after we swap s[i] and t[i], the character a moves from s[i] to t[i], and t[i] becomes a. However, we need t[j] == a for the second swap. The j we selected must have t[j] == a in the current state at the moment before the second swap. If we selected j based on original state, after the first swap, t[j] remains unchanged because we haven't touched j yet. So t[j] still equals a. So it's fine. Also, we must be careful that we don't accidentally select j that might have been altered earlier. But since we are iterating i left to right, and we only modify positions >= i (j>i), when we are at i, all positions < i are fixed and not touched again. Positions > i may have been modified by previous steps? Wait, when processing earlier indices k < i, we might have modified some positions j > k. Those j could be greater than i as well. So at step i, some positions > i may have been changed already. So the state of those positions is not the initial state. However, our existence argument for j > i with either s_j == a or t_j == a relies on the invariant after processing up to i-1. That invariant ensures there is at least one a in the suffix (i..n) aside from the a at s_i? Actually we need an a in positions i+1..n (since we exclude i). Because we already accounted that s_i = a. The invariant that total a in suffix i..n is even, and at i we have one a in s, implies there is at least one more a in i+1..n. However, that a could be in either s or t. So existence is guaranteed in the current state after previous modifications, not necessarily in the original state. So we need to search in the current strings for such j. That's fine.
Thus algorithm: iterate i from 0 to n-1:
if s[i] == t[i]: continue
a = s[i]
find j in range(i+1, n) such that s[j] == a
if exists: swap(s[j], t[i]); record (j+1, i+1)
else:
# find j in range(i+1, n) such that t[j] == a
# it must exist; swap(s[i], t[i]); swap(s[i], t[j]); record both
But careful: In the else case, we must ensure that we find t[j] == a. But what if there is also s[j] == a? That would have been caught in the first condition. So else case implies there is no s[j] == a for j>i, but there must be some t[j] == a. However, could it happen that there is also a t[j] == a but we inadvertently pick j such that after first swap something goes wrong? It should work.
We also need to consider the possibility that j = i? But we only look for j>i.
Now, let's test this algorithm on some cases.
Example 1: s="souse", t="houhe". We'll simulate. n=5, 0-index.
i=0: s[0]='s', t[0]='h' (a='s', b='h'). Look for j>0 with s[j]=='s'. s[1]='o', s[2]='u', s[3]='s', s[4]='e' -> s[3]=='s' found at j=3. So swap(s[3], t[0]). s[3] is 's', t[0] is 'h'. After swap: s[3] becomes 'h', t[0] becomes 's'. Now strings: s: s o u h e? Wait s[0]='s', s[1]='o', s[2]='u', s[3]='h', s[4]='e' => "souhe". t: originally h o u h e, after swap t[0]='s', so t: s o u h e? Actually t[0]='s', t[1]='o', t[2]='u', t[3]='h', t[4]='e' => "souhe". Now both become "souhe"? Check: s="souhe"? Actually s: s,o,u,h,e => "souhe". t: s,o,u,h,e => "souhe". They are equal already! But we only did one swap. However original output had 1 swap (1,4). That matches our swap: (j+1=4, i+1=1) => 4,1? Actually they output "1 4" meaning swap s1 with t4. But our swap was s4 with t1? Let's check indices: In problem, they swap s_i and t_j. Our swap was s[3] (index 4 in 1-based) with t[0] (index 1). That's (4,1) not (1,4). But they output "1 4". That's swapping s1 with t4. Both achieve the same effect? In example, they swapped s1 with t4 and got "house" for both. Our result is "souhe". So both are valid; we don't need to match exactly. So algorithm works.
Now we see after i=0, all positions become equal? Let's check: s="souhe", t="souhe". Indeed all equal. So we can stop early? But algorithm would continue but see all s[i]==t[i] for i>=1. So done.
Example 4: s="abc", t="bca". n=3.
i=0: s[0]='a', t[0]='b'. Find j>0 with s[j]=='a'. s[1]='b', s[2]='c' -> none. So else: find t[j]=='a'. t[1]='c', t[2]='a'? Actually t[2]='a'? t="bca": indices:0:'b',1:'c',2:'a'? Wait t[2] is 'a'? t[0]='b', t[1]='c', t[2]='a'? But string "bca": positions:1:b,2:c,3:a. So t[2]='a'. So j=2. Then do: swap(s[0], t[0]) first. s becomes "bbc"? Actually s[0]='a', t[0]='b'. After swap: s[0]='b', t[0]='a' -> s="bbc"? Wait s[0]='b', s[1]='b'? Actually s originally "abc": a,b,c. After first swap: s[0] becomes b, so s="bbc"? No, s[1]='b', s[2]='c', so s="bbc"? s[0]='b', s[1]='b', s[2]='c' => "bbc". t becomes: t[0]='a', t[1]='c', t[2]='a'? originally t="bca": b,c,a; after swap t[0]='a', so t="aca"? Actually t[0]='a', t[1]='c', t[2]='a' => "aca". Then second swap: swap(s[0], t[2]). s[0] currently 'b', t[2] currently 'a'. After swap: s[0]='a', t[2]='b'. So s becomes "abc"? s[0]='a', s[1]='b', s[2]='c' => "abc". t becomes: t[0]='a', t[1]='c', t[2]='b' => "acb". Now after i=0, s[0]='a', t[0]='a' => fixed. But strings are not fully equal: s="abc", t="acb". Next i=1: s[1]='b', t[1]='c'. Find j>1 with s[j]=='b'. s[2]='c', not b. So else: find t[j]=='b'. t[2]='b' (since we set t[2]='b' earlier). j=2. Then do: swap(s[1], t[1]) first. s[1]='b', t[1]='c' -> after swap: s[1]='c', t[1]='b' -> s="acc"? Actually s[0]='a', s[1]='c', s[2]='c'? Wait s[2]='c' unchanged, so s="acc"? No s[2]='c', so s becomes "acc"? s[0]='a', s[1]='c', s[2]='c' => "acc". t becomes: t[0]='a', t[1]='b', t[2]='b' => "abb". Then second swap: swap(s[1], t[2]). s[1]='c', t[2]='b' -> after swap: s[1]='b', t[2]='c'. So s becomes "abc": s[0]='a', s[1]='b', s[2]='c'. t becomes: t[0]='a', t[1]='b', t[2]='c' => "abc". Now both equal. Swaps recorded: first for i=0: (1,1) and (1,3)? Actually indices: i=0 -> first swap (s[0], t[0]) => (1,1). Second swap (s[0], t[2]) => (1,3). For i=1: first swap (s[1], t[1]) => (2,2). Second swap (s[1], t[2]) => (2,3). So total 4 swaps: (1,1), (1,3), (2,2), (2,3). That's 4 swaps, but example output shows 3 swaps: (1,2), (3,1), (2,3). So our sequence uses 4 swaps, still within 2n=6. So it's acceptable. Output doesn't have to be minimal. So algorithm works, but may produce up to 2 per mismatch. In this case there were 2 mismatches? Actually initially mismatches at i=0 and i=1 and i=2? Let's see: original s=abc, t=bca: i=0 a vs b, i=1 b vs c, i=2 c vs a -> all three mismatches. Our algorithm used 2 swaps for i=0, 2 for i=1, and then i=2 would be checked but after i=1, strings become equal? After i=1 we got both equal, so i=2 is fine. So we used 4 swaps, which is ≤ 2n=6. So okay.
But note: In some cases, the algorithm might use 2 swaps for each mismatch,