Balanced Reversals

 Chef is given a binary string 

A of length N. He can perform the following operation on A any number of times:

  • Choose L and R (1LRN), such that, in the substring A[L,R], the number of 1s is equal to the number of 0s and reverse the substring A[L,R].

Find the lexicographically smallest string that Chef can obtain after performing the above operation any (possibly zero) number of times on A.

String X is lexicographically smaller than string Y, if either of the following satisfies:

  • X is a prefix of Y and XY.
  • There exists an index i such that Xi<Yi and Xj=Yj,j such that 1j<i.

Input Format

  • First line will contain T, the number of test cases. Then the test cases follow. Each test case contains two lines.
  • The first line contains the integer N, the length of the binary string.
  • The second line contains the binary string A.

Output Format

For each test case, print the lexicographically smallest binary string that can be obtained after performing the operation any (possibly zero) number of times.

Constraints

  • 1T100
  • 1N105
  • Sum of N over all test cases does not exceed 2105.

Sample Input 1 

2
5
01100
4
0000

Sample Output 1 

00011
0000

Explanation

Test Case 1: Chef can choose L=2 and R=5. The chosen substring, A[2,5]=1100. On reversing this, we get 0011. Thus, the final string is A=00011. Note that this is the lexicographically smallest string possible.

Test Case 2: Since the string is already lexicographically minimum, Chef does not need to apply any operation.

Previous
Next Post »