We need to parse the problem carefully. The problem description: "One night Mr.X and Mr.Y were playing a game. To test Mr.Y's intelligence Mr.X gives him an interesting problem to solve. He gives him a number N and asks him to swap the digits in the number such that a smallest number M is obtained without leading zeroes. Mr.Y thinks this is an easy problem to solve and immediately gives the answer. The number can be large therefore Mr.X wants to check Mr.Y's answer by writing a program for this problem. Since Mr.X is not so good in programming therefore he asks for your help in doing so."
Input format: first line T. Then for each test case: first line integer N, second line integer M (Mr.Y's answer). Output "AC" if M is correct (i.e., M is the smallest number obtainable by swapping digits of N, without leading zeroes), else "WA".
Constraints: 1 ≤ T ≤ 100, 1 ≤ N ≤ 10^9, 1 ≤ M ≤ 10^9.
Example:
Input:
2
310
103
1
2
Output:
AC
WA
Explanation: For N=310, digits: 3,1,0. Swapping digits can yield numbers: 310, 301, 130, 103, 013 (leading zero -> invalid), 031 (invalid). Smallest without leading zeroes is 103. So M=103 is correct -> AC. Second test: N=1, digits only 1, smallest is 1. Mr.Y gave 2 -> WA.
Thus the problem is straightforward: For each N, we need to compute the smallest possible number after swapping its digits, disallowing leading zeros, and compare to M.
But careful: The problem statement says "swap the digits in the number such that a smallest number M is obtained without leading zeroes." It does not say we can only swap two digits; it says "swap the digits" which likely means we can reorder the digits arbitrarily (by swapping them). Usually such problems: given a number, rearrange its digits to get smallest possible number without leading zero. That's essentially sorting digits ascending, then ensuring first digit is not zero. If the smallest digit is zero, we need to take the smallest non-zero digit and put it first, then the rest in ascending order (including zeros). So algorithm: get digits of N as string. Sort digits ascending. If smallest digit is not '0', then M_expected is the sorted string. Else, find the smallest non-zero digit, place it first, then all zeros, then the remaining digits sorted ascending.
Edge Cases: N can have duplicate digits. Leading zeros: if all digits are zero? But N >=1 and no leading zeros, so N has at least one non-zero digit. But it's possible that N is like 0? Not according to constraints (N>=1). But maybe N can be 0? Constraint says 1 ≤ N, so no. So at least one non-zero digit.
Check constraints: N up to 1e9, so up to 10 digits. So it's fine.
Implementation: For each test case:
- Read N as string? But input is integer without leading zeroes. However, reading as string is easier for digit manipulation. But careful: N may be up to 10^9, which fits in int. We'll read as string to avoid conversion issues. But also note that there may be multiple test cases, each with two lines. The sample input: "2\n310\n103\n1\n2". After first test case (N=310, M=103), next test case: N=1, M=2. So we just read sequentially.
Potential pitfalls: The problem statement might have large T but N is small. Just compute expected M and compare as string (or as integer if you want). Since M is provided as integer, but leading zeros are not allowed for M, so it's fine.
Now, implement solve().
But careful: There may be multiple lines with spaces? Probably not.
Pseudo:
def smallest_number(n_str):
digits = list(n_str)
digits.sort()
if digits[0] != '0':
return ''.join(digits)
# find first non-zero
# find index of smallest non-zero
# Approach: iterate from left, find first > '0'
for i, d in enumerate(digits):
if d != '0':
# swap first zero with this digit
# Actually we can construct: first char = d, then digits[0:i] + digits[i+1:] sorted? But after moving d to front, we need the rest sorted ascending.
# Since digits are already sorted, if we take the first non-zero digit and place it front, the rest (including zeros and other digits) should be in ascending order.
# So result = d + ''.join(digits[:i]) + ''.join(digits[i+1:])
# But digits[:i] are all zeros (since sorted, zeros come first). digits[i+1:] are the remaining digits sorted.
# So it's fine.
# However, note that there could be multiple zeros. So digits[:i] are zeros, digits[i+1:] are remaining.
return d + ''.join(digits[:i]) + ''.join(digits[i+1:])
# Shouldn't happen because at least one non-zero.
return n_str
Then for each test:
n_str = input().strip()
m_str = input().strip()
expected = smallest_number(n_str)
if expected == m_str:
print("AC")
else:
print("WA")
Edge Cases: When N is single digit, smallest number is itself.
Potential nuance: Swapping digits means we can swap any two digits any number of times, effectively permuting the digits. So our approach is correct.
But careful: The problem statement says "swap the digits in the number such that a smallest number M is obtained without leading zeroes." Could it mean that we are allowed to swap exactly two digits? That would be different. But typical "swap digits" sometimes means you can swap any two digits, but you can only do one swap? However, the phrasing "swap the digits" could be ambiguous. But the example: N=310, the smallest after swapping digits (presumably any number of swaps, i.e., any rearrangement) is 103. If only one swap allowed, possible numbers: swap 3 and 1 -> 130; swap 3 and 0 -> 013 (invalid) -> 13? Actually swapping 3 and 0 yields 013, which as integer is 13, but with leading zero it's not allowed; so valid numbers from single swap (and ignoring leading zero) would be 130 and 301? Let's list: Starting 310, swap positions: (1st,2nd) -> 130; (1st,3rd) -> 013 -> 13 (but as number without leading zero it's 13, but if we consider representation, leading zero is not allowed, so invalid). (2nd,3rd) -> 301. So valid numbers are 130, 301. The smallest among these is 130. But Mr.Y's answer is 103, which is not achievable with a single swap. So the example suggests that multiple swaps or arbitrary reordering is allowed. So our interpretation is correct.
Thus solution as above.
Now, we should double-check constraints: N up to 10^9, but string length <=10. T up to 100. So trivial.
But we must be careful with input reading: There might be blank lines? Usually not. We'll read using sys.stdin.
We'll produce final answer.
But wait: The example input has 2 test cases. After the first test case, there is "1" and "2". So reading line by line is fine.
Potential pitfalls: When reading large T, we need to ensure we don't strip incorrectly.
We'll implement:
import sys
def main():
data = sys.stdin.read().split()
# According to format, first token is T, then for each test: N and M as strings.
# Since numbers have no leading zeros, they are read as tokens.
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = next(it)
m = next(it)
expected = smallest_number(n)
out.append("AC" if expected == m else "WA")
sys.stdout.write("\n".join(out))
def smallest_number(s):
# as above
digits = sorted(s)
if digits[0] != '0':
return ''.join(digits)
# find first non-zero
i = 0
while i < len(digits) and digits[i] == '0':
i += 1
# i now points to first non-zero
# swap: first char = digits[i], then zeros count i, then rest after i
# Actually we can construct: digits[i] + ''.join(digits[:i]) + ''.join(digits[i+1:])
return digits[i] + ''.join(digits[:i]) + ''.join(digits[i+1:])
But wait: If there are multiple non-zero digits, after moving the smallest non-zero to front, the rest should be sorted ascending. Since digits is sorted, digits[:i] are all zeros, digits[i+1:] are sorted ascending (including possibly other non-zero digits). So it's correct.
Alternative: we can just find the smallest non-zero digit, remove one occurrence, then sort the rest and prepend that digit.
We'll code accordingly.
Now, I'll think about edge cases: N=100. digits sorted: 0,0,1. Smallest non-zero is 1, put it front: result = 1 + '00' = 100. So smallest is 100. That's correct (since we can't get 001). Good.
N=1010: digits: 0,0,1,1 -> smallest non-zero is 1 -> result: 1 + '001' = 1001. But could we get 1001? Let's check permutations: smallest without leading zero: we need first digit smallest non-zero (1), then the rest sorted ascending: 0,0,1 => 1001. That seems smallest: 1001 < 1010, 1100, etc. Yes.
Now, M is provided as integer without leading zero, so string comparison works.
One nuance: Could M have leading zeros? It says "without leading zeroes" for M? The second line contains integer M, presumably also without leading zeroes. So fine.
Double-check: The problem statement: "The first line of each test case contains one integer N without leading zeroes. The second line will contain one integer M — Mr.Y's answer." So M also is integer, no leading zeros. So we compare strings directly.
Thus solution is simple.
But wait, could there be any trick? Possibly Mr.Y's answer might be correct even if it's not the minimal but if it's achievable by swapping digits? The problem says: "swap the digits in the number such that a smallest number M is obtained". So M must be the smallest possible. So if Mr.Y gives a number that is reachable but not smallest, it's wrong. So we only accept the minimal.
Thus our logic.
Time complexity: O(T * d log d) where d is number of digits (max 10). So trivial.
Now, I'll produce final answer.
But before final, I'll test with sample:
Input: 2 310 103 1 2
t=2, first test: n='310', digits sorted: ['0','1','3'], first is '0', i=1 (digit '1'), result='1'+''.join(['0'])+''.join(['3']) = "1"+"0"+"3" = "103". matches m='103' -> AC.
Second: n='1', digits sorted ['1'], first not zero -> '1'. m='2' -> not equal -> WA.
Matches.
Edge case: N=10, digits sorted: ['0','1'], first zero, i=1 (digit '1'), result='1'+'0'='10'. So smallest is 10 itself. So if M=10 -> AC. If M=01 (not valid input) but M is integer, so '1'? Actually 01 as integer would be 1? But they won't give leading zeros. So fine.
Now, confirm that swapping digits can produce any permutation? Yes.
Thus solution done.
But sometimes problem expects you to treat swap as only one swap? However example clearly indicates multiple swaps allowed. So good.
Now, produce final answer in Python code.
Potential issues: Reading input with sys.stdin.read().split() will split on whitespace, which is fine because numbers are separate lines. Ensure we convert T correctly.
Alright.
Now, final answer. ```python
import sys
def smallest_number(s: str) -> str:
"""Return the smallest number obtainable by permuting the digits of s,
without leading zeros."""
digits = sorted(s) # ascending order
if digits[0] != '0':
return ''.join(digits)
# find the first non‑zero digit (smallest non‑zero)
i = 0
while i < len(digits) and digits[i] == '0':
i += 1
# digits[i] is the smallest non‑zero digit.
# Put it first, then all zeros, then the remaining digits.
return digits[i] + ''.join(digits[:i]) + ''.join(digits[i+1:])
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = next(it) # N as string
m = next(it) # M as string
expected = smallest_number(n)
out_lines.append("AC" if expected == m else "WA")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()