Maximize permutations
##### Submissions: 510   Accuracy: 17.04%   Difficulty: Easy   Marks: 2

Given two numbers N and M. Construct maximal number by permuting (changing order) the digits of N, not exceeding M. It is allowed to leave N as it is.

Input:
The first line of the input contains T i.e number of test cases. Each line of the test case contains Two numbers N, M  separated by space.

Output:
Each new line of the output contains only one single integer i.e Maximize Permutation if possible else -1.

Constraints:
1 <= T <= 500
1 <= N <= 105
1 <= M <= 105

Example:
Input:
2
123 222
3921 10000

Output:
213
9321

Explanation:
Tese Case 1: here are total 3! permutations possible for N = 123, But the only permutation that satisfies the given condition is 213.

#### ** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Nishant Singh
Author: Nishant_Singh

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.