CF BUDDY
← Problems·

1328C · Ternary XOR

1200 · greedy, implementation

Problem: A number is ternary if it contains only digits 00, 11 and 22. For example, the following numbers are ternary: 10221022, 1111, 2121, 20022002.

You are given a long ternary number xx. The first (leftmost) digit of xx is guaranteed to be 22, the other digits of xx can be 00, 11 or 22.

Let's define the ternary XOR operation \odot of two ternary numbers aa and bb (both of length nn) as a number c=abc = a \odot b of length nn, where ci=(ai+bi)%3c_i = (a_i + b_i) \% 3 (where %\% is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by 33. For example, 1022211021=2121010222 \odot 11021 = 21210.

Your task is to find such ternary numbers aa and bb both of length nn and both without leading zeros that ab=xa \odot b = x and max(a,b)max(a, b) is the minimum possible.

You have to answer tt independent test cases.

Input Format: The first line of the input contains one integer tt (1t1041 \le t \le 10^4) — the number of test cases. Then tt test cases follow. The first line of the test case contains one integer nn (1n51041 \le n \le 5 \cdot 10^4) — the length of xx. The second line of the test case contains ternary number xx consisting of nn digits 0,10, 1 or 22. It is guaranteed that the first digit of xx is 22. It is guaranteed that the sum of nn over all test cases does not exceed 51045 \cdot 10^4 (n5104\sum n \le 5 \cdot 10^4).

Output Format: For each test case, print the answer — two ternary integers aa and bb both of length nn and both without leading zeros such that ab=xa \odot b = x and max(a,b)max(a, b) is the minimum possible. If there are several answers, you can print any.

Sample Cases

Case 1

Input

4
5
22222
5
21211
1
2
9
220222021

Output

11111
11111
11000
10211
1
1
110111011
110111010

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback